Why use ClickDetector?
ClickDetector provides a basic world interaction event. Its MouseClick handler receives the Player who clicked; important actions must still be checked on the server.
Try it in Roblox Studio
Create a Part named ButtonPart in Workspace. Put this Script in ServerScriptService and test by clicking the part in a Studio client.
Syntax
detector.MouseClick:Connect(function(player) ... end)Examples
ClickDetector
Print which player clicked a nearby 3D button.
local part = workspace:WaitForChild("ButtonPart")
assert(part:IsA("BasePart"))
local detector = Instance.new("ClickDetector")
detector.MaxActivationDistance = 16
detector.Parent = part
detector.MouseClick:Connect(function(player)
print(player.Name, "clicked", part.Name)
end)- The detector is parented to the clickable Part.
- MaxActivationDistance limits the intended interaction range.
- MouseClick supplies the clicking Player. A real purchase, reward, or privileged action needs additional server-side validation.
Best practices
- Validate rewards and sensitive interactions on the server.
- Use ProximityPrompt if an explicit keyboard/touch interaction hint suits the design better.
Runtime & versions
Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Luau documentation