Coin Door
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 17 of 25.
An if can do more than check for nil. It can compare numbers, and an else part runs when the comparison is false:
local score = player.leaderstats.Score.Value
if score >= 10 then
print("You win!")
else
print("Keep going")
endThe comparison operators are == (equal), ~= (not equal), <, >, <= and >=. Note the double ==: a single = puts a value in a variable, it does not compare.
>= 3 means "3 or more"; > 3 means "more than 3", so exactly 3 would not pass.
Challenge
MediumOpen the wooden door only for players with 3 coins or more.
- Under the
coinsline, if the player has3or more, open the door:CanCollidefalseandTransparency0.5. - Otherwise (
else), printYou need 3 coins.
Press Play: Output says You need 3 coins until you collect all three, then the blue pad opens the door. Then Check.
Try it yourself
local Players = game:GetService("Players")
local door = script.Parent
local pad = workspace.DoorPad
pad.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
local coins = player.leaderstats.Coins.Value
-- 3 coins or more: open the door
-- otherwise: print "You need 3 coins"
end
end)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler