Coin Door
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 16 of 24.
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
MediumThree coins float on the right, and a wooden door blocks the wall. The world counts the coins for you. This script is in the door, and its Touched function runs when a player steps on the blue pad in front of it.
- If the player has
3or more Coins, open the door:CanCollidefalseandTransparency0.5. - Otherwise, print exactly
You need 3 coins.
Press Play, try the pad first, collect the coins and try again. 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