Learn How to Code Games in Roblox Studio¶
Links¶
Course Topics¶
Studio: parts, physics, terrain, lighting, atmosphere, effects, assets
Game structure, collaboration, client-server model
Lua: collisions, humanoids, score, GUIs, animation, sounds, combat, teleporting, DataStores, module scripts, camera
Lua Basics (Section 2)¶
Program to Print the String¶
--- print("Hello Senthil")
--
local myString = "Hi my name is Senthil"
print(typeof(myString))
print(myString)
-- This is a number variable
local myInteger = 32.67
print(typeof(myInteger))
print(myInteger)
local myBoolean = true
print(typeof(myBoolean))
print(myBoolean)
Program with Conditional Statement¶
local myString = "Hi my name is Senthil"
local myInteger = 32.67
local myBoolean = true
print(myString)
myString = "My name is now Darth Vader"
print(myString)
if myBoolean == true then
print("My boolean is true")
else
print("My boolean is false")
end
if myInteger > 30 then
print("My integer is greater than 30")
else
print("My integer is less than 30")
end
if (1 + 1 == 3) then
print("1 + 1 is 3")
else
print("1 + 1 is not 3. It is 2")
end
Program with Conditional Statements and Boolean¶
local age = 15
local movieRating = "M"
if age >= 15 then
print("You can watch the M rated movie")
else
print("You are too young to watch the M rated movie")
end
movieRating = "G"
if movieRating == "G" then
print("You can watch the G rated movie")
end
movieRating = "M"
if age >= 15 and movieRating == "M" then
print("You can watch the M rated movie")
end
Functions¶
local myName = "Senthil"
-- The dot dot symbol is called concatenation operator
local function sayMyName()
print("My name is " .. myName)
end
sayMyName()
myName = "Kumaran"
sayMyName()
Functions with Parameters¶
local function sayMyName(myName, age)
print("Hello, " .. myName)
print("I am " .. age .. " years old")
end
sayMyName("Senthil", 42)
Parameter Strings¶
local function addNums(num1, num2)
return num1 + num2
end
local answer = addNums(1, 2)
print(answer)
Calculator with String¶
local function addNums(n1, n2)
return n1 .. " + " .. n2 .. " = " .. (n1 + n2)
end
local answer = addNums(1, 2)
print(answer)
For Loop¶
local myNum = 0
for i = 1, 10, 1 do
myNum = myNum + 1
print(myNum)
end
While Loop¶
local myNum = 0
local myBool = true
while myBool == true do
myNum = myNum + 1
print(myNum)
if myNum == 10 then
myBool = false
end
end
print("The while loop has ended", myBool)
print("The final value of myNum is", myNum)
Table Example¶
-- An empty table
local myTable = {}
-- Add an item to the table
myTable[1] = "banana"
table.insert(myTable, "orange")
print(table)
for i=1, #myTable do
print(myTable[i])
end
local boxTable = workspace.boxModel:GetChildren()
print(boxTable)
boxTable[4].BrickColor = BrickColor.new("Really red")
All Steps Example¶
local allSteps = workspace.magicStepsModel.stepModel:GetChildren()
while wait(1) do
local randNum = math.random(1, #allSteps)
local randStep = allSteps[randNum]
for i=0, 1, .2 do
task.wait(.1)
randStep.Transparency = i
end
randStep.CanCollide = false
wait(.5)
for i=1, 0, -0.2 do
wait(.1)
randStep.Transparency = i
end
randStep.CanCollide = true
end
Part & Workspace¶
local myPart = script.Parent
myPart.BrickColor = BrickColor.new("Bright red")
myPart.BrickColor = BrickColor.Random()
local myPart = game.Workspace.Part
myPart.Transparency = 0.5
local myBridge = game.Workspace.Bridge
myBridge.Grass.BrickColor = BrickColor.Random()
Tip: When creating a part, make it anchored.
Touch Events¶
local myPart = script.Parent
myPart.BrickColor = BrickColor.Random()
local function partTouchedFunction(partTouched)
print(partTouched)
end
myPart.Touched:Connect(partTouchedFunction)
Changing Brick Color with Touch¶
local myPart = script.Parent
-- debounce
local db = true
local function partTouchedFunction(partTouched)
if db then
db = false
myPart.BrickColor = BrickColor.Random()
wait(2)
db = true
end
end
myPart.Touched:Connect(partTouchedFunction)
Inline Touch Function¶
local myPart = script.Parent
-- debounce
local db = true
myPart.Touched:Connect(function(partTouched)
if db then
db = false
myPart.BrickColor = BrickColor.Random()
wait(2)
db = true
end
end)
Player Touch the Part¶
-- Player = This is you!
-- Humanoid - A little robot
-- Character - This is how you look in the game.
--
local myPart = script.Parent
-- debounce
local db = true
myPart.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and db then
local player = game.Players:GetPlayerFromCharacter(character)
-- game.Players:FindFirstChild(character.Name)
db = false
myPart.BrickColor = BrickColor.Random()
print(player.Name .. " touched the part!")
wait(2)
db = true
end
end)
-- Test this. Player can change the color of the part by touching it.
-- Moving another block against it will not change the color.
Pick Up the Part¶
-- Create a Spawn point.
-- Create a part to pick up. Rename it to "Pickup"
-- Anchored
-- Add a Script and rename it to pickupScript
--
--
local pickup = script.Parent
local db = true
pickup.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and db then
local player = game.Players:GetPlayerFromCharacter(character)
-- game.Players:FindFirstChild(character.Name)
db = false
pickup.Transparency = 1
pickup.CanCollide = false
print(player.Name .. " picked up the part!")
wait(2)
pickup.Transparency = 0
pickup.CanCollide = true
db = true
end
end)
Sign to Pick Up the Part¶
-- Create a Spawn point.
-- Create a part to pick up. Rename it to "Pickup"
-- Anchored
-- Add a Script and rename it to pickupScript
--
local sign = game.Workspace.signModel.backboard.SurfaceGui.Frame.TextLabel
local pickup = script.Parent
local db = true
pickup.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and db then
local player = game.Players:GetPlayerFromCharacter(character)
-- game.Players:FindFirstChild(character.Name)
db = false
pickup.Transparency = 1
pickup.CanCollide = false
sign.Text = "You have picked up the item!"
print(player.Name .. " picked up the part!")
wait(5)
pickup.Transparency = 0
pickup.CanCollide = true
sign.Text = "Pick up the part!"
db = true
end
end)
Camping Project (Section 3)¶
Leaderboard¶
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end)
end)
Log Pickup¶
-- logScript
local log = script.Parent
local db = true
log.Touched:Connect(function(hit)
local character = hit.Parent
local hum = char:FindFirstChild("Humanoid")
if db and hum then
db = false
local plr = game.Players:FindFirstChild(char.Name)
local pStats = plr:WaitForChild("leaderstats")
log.Transparency = 1
wait(3)
log.Transparency = 0
db = true
end
end)
Roblox Programming — 2023¶
Pass the Ice Wall¶
-- Add this 1
local wall = workspace.iceWall
local myName = "Bill"
local myAge = 12
local isAllowedToGoCamping = true
-- Add this 3
task.wait(5)
-- Add this 2
if myName == "Bill" then
print("Yes your name is ", myName)
wall.Transparency = 0.3
else
warn("No your name is not Bill")
end
task.wait(5)
if myAge >= 10 then
print("Yes you are ", myAge, " you are old enough to go camping")
wall.Transparency = 0.6
end
if isAllowedToGoCamping == true then
print("Yes you are allowed to go camping")
wall.Transparency = 1
wall.CanCollide = false
end