Roblox click detector script
Make a part do something when a player clicks or taps it. The main script gives a coin per click with a cooldown; the variations add a light switch and hover effects.
ScriptUpdated November 2, 2026
Where to put it
Script — Inside the part (Workspace > Button > Script). It runs on the server, so players can't change what it does.
- Insert an anchored Part, then insert a
ClickDetectorand a Script inside it. - For the coin reward, players need
leaderstats > Coins— the leaderboard script creates it.
The script
local Players = game:GetService("Players")
local part = script.Parent
local detector = part:WaitForChild("ClickDetector")
local COOLDOWN = 1 -- seconds between clicks per player
local lastClick = {}
detector.MaxActivationDistance = 32
detector.MouseClick:Connect(function(player)
local now = os.clock()
if lastClick[player] and now - lastClick[player] < COOLDOWN then
return
end
lastClick[player] = now
local leaderstats = player:FindFirstChild("leaderstats")
local coins = leaderstats and leaderstats:FindFirstChild("Coins")
if coins then
coins.Value += 1
end
part.Color = Color3.fromHSV(math.random(), 0.7, 1)
end)
Players.PlayerRemoving:Connect(function(player)
lastClick[player] = nil
end)How it works
MouseClickfires on the server with the player who clicked. Taps on phones and tablets fire it too.- The cooldown is checked on the server, so an auto-clicker can't earn more than one coin per second.
MaxActivationDistanceis how close (in studs) the player must be to click it.
Variations
Light switch
Click to turn a lamp on and off. Put a PointLight in the lamp part and point lamp at it.
local detector = script.Parent:WaitForChild("ClickDetector")
local lamp = workspace:WaitForChild("Lamp")
local light = lamp:WaitForChild("PointLight")
detector.MouseClick:Connect(function()
light.Enabled = not light.Enabled
lamp.Material = light.Enabled and Enum.Material.Neon or Enum.Material.SmoothPlastic
end)Hover highlight (only for the player hovering)
A LocalScript in StarterPlayerScripts. Hover effects belong on the client, so other players don't see your mouse.
local button = workspace:WaitForChild("Button")
local detector = button:WaitForChild("ClickDetector")
local highlight = Instance.new("Highlight")
highlight.FillTransparency = 1
highlight.Enabled = false
highlight.Parent = button
detector.MouseHoverEnter:Connect(function()
highlight.Enabled = true
end)
detector.MouseHoverLeave:Connect(function()
highlight.Enabled = false
end)ClickDetector or ProximityPrompt?
Use a ClickDetector for things players point at from a distance (buttons, switches, clicker objects). Use a ProximityPrompt for things they walk up to (doors, pickups, NPCs): it shows the key, works with gamepads and is clearer on phones.
Common mistakes
- Giving rewards from a LocalScript: the change isn't saved on the server and exploiters can fire it. Reward in a server Script.
- Another part (often a transparent one) sits in front of the button and catches the click. Move it, or turn off its
CanCollideandCanQuery. - The player is too far away — the default range is 32 studs.
- No cooldown on rewards: auto-clickers will farm your currency.
FAQ
Why is my click detector not working in Roblox?
Check that the ClickDetector is inside the part (or a Model), that you are within MaxActivationDistance, that no other part blocks the click, and that you use a Script listening to MouseClick. Red errors in the Output window point to typos.
Does a ClickDetector work on mobile?
Yes. Tapping the part fires MouseClick, the same as a mouse click.
How do I get the player who clicked?
MouseClick passes the player as its first argument: detector.MouseClick:Connect(function(player) ... end).