Menu
CoddyTech

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")
end

The 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 icon

Challenge

Medium

Open the wooden door only for players with 3 coins or more.

  1. Under the coins line, if the player has 3 or more, open the door: CanCollide false and Transparency 0.5.
  2. Otherwise (else), print You 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game Scripting

Practice on your own: Online Lua compiler