Menu
CoddyTech

Click Me

Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 19 of 24.

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

The round lamp has a ClickDetector, and this script is inside the lamp. Make it a switch: every click flips the lamp between on and off.

  • It starts off: Plastic
  • on is Neon, off is Plastic

Press Play and click the lamp a few times, 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)
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