Syntax
prompt.Triggered:Connect(function(player)
-- Validate and perform the action on the server.
end)Examples
Offer a nearby interaction
Create an anchored Part named LessonMarker in Workspace. Script in ServerScriptService adds a prompt. It checks distance and life state again before printing, with a per-player cooldown. This is a demonstration, not a reward system.
local Players = game:GetService("Players")
local marker = workspace:WaitForChild("LessonMarker")
assert(marker:IsA("BasePart") and marker.Anchored)
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = "Inspect"
prompt.MaxActivationDistance = 10
prompt.Parent = marker
local last = {}
prompt.Triggered:Connect(function(player)
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if not prompt.Enabled or not root or not root:IsA("BasePart") or not humanoid or humanoid.Health <= 0 then return end
local distance = (root.Position - marker.Position).Magnitude
if distance ~= distance or distance > 10 then return end
local now = os.clock()
if now - (last[player] or -math.huge) < 1 then return end
last[player] = now
print(player.Name, "inspected the marker")
end)
Players.PlayerRemoving:Connect(function(player) last[player] = nil end)Debounce a cosmetic touch reaction
Alternative Script in ServerScriptService for the same anchored LessonMarker. This changes only its color. A single object-wide debounce is appropriate here because the effect belongs to the marker, not an individual player.
local marker = workspace:WaitForChild("LessonMarker")
local busy = false
marker.Touched:Connect(function(otherPart)
local character = otherPart:FindFirstAncestorOfClass("Model")
if busy or not character or not character:FindFirstChildOfClass("Humanoid") then return end
busy = true
marker.Color = Color3.fromRGB(80, 180, 120)
task.delay(1, function()
if marker.Parent then marker.Color = Color3.fromRGB(160, 160, 160) end
busy = false
end)
end)Best practices
- Use Triggered for a completed prompt interaction and still validate important conditions on the server.
- A client-side debounce improves presentation but cannot enforce a server cooldown.
- Reset state on every completion/error path; a flag that stays true can permanently disable an interaction.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.Server authority, validation and rate limits
A valid-looking remote call can still be an impossible game action. Validate both the shape of a request and whether the sender may perform it now. This example lets a nearby living player request a visual switch; it does not trust a client position, target Instance or reward.task scheduling and RunService
Use task scheduling for delayed or deferred work, and RunService when a feature genuinely depends on simulation or rendering steps. A delay is a minimum scheduling interval rather than an exact clock. Avoid doing work every frame when an event can report the change.Raycasting and hit results
A raycast asks what a line segment intersects. Its direction vector includes the distance to search, not just a unit direction. A miss returns nil; a hit provides an Instance, position and surface normal. Filters determine what the query ignores.