Click Me
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 20 of 25.
A ClickDetector inside a part makes it clickable: the mouse turns into a hand over it, and its MouseClick event fires with the player who clicked. Players can click from up to 32 studs away.
local bell = script.Parent
bell.ClickDetector.MouseClick:Connect(function(player)
print(player.Name .. " rang the bell")
end)A switch has to remember whether it is on. Keep that in a variable outside the function, and flip it with not: not true is false and not false is true.
local open = false
open = not open -- now true
open = not open -- false againChallenge
MediumMake the round lamp a switch: every click turns it on or off.
- Inside the click function, flip
onto its opposite. - Then, if
on, set the lamp's Material toNeon, otherwise toPlastic. It starts off.
Press Play and click the lamp a few times: it glows, goes dark, and glows again. Then Check.
Try it yourself
local lamp = script.Parent
local on = false
lamp.ClickDetector.MouseClick:Connect(function(player)
-- Flip on, then make the lamp Neon when on, Plastic when off
end)
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