Menu
CoddyTech

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 again
challenge icon

Challenge

Medium

Make the round lamp a switch: every click turns it on or off.

  1. Inside the click function, flip on to its opposite.
  2. Then, if on, set the lamp's Material to Neon, otherwise to Plastic. 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

Practice on your own: Online Lua compiler