Below is one way to add a simple countdown-GUI so that, once the player steps on your “teleport” part, they’ll see “Teleporting in 5…4…3…” and so on. The idea is:
- The server script fires a RemoteEvent to the client when the countdown starts/stops.
- A LocalScript in
StarterGui listens to that event and drives a small ScreenGui with a TextLabel to show the remaining seconds.
1) Server-side changes
In your existing ServerScript (e.g. in ServerScriptService), do the following changes:
a) At the top, create (or reference) a RemoteEvent in ReplicatedStorage:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- If you don't already have the event, create it:
local teleportCountdownEvent = ReplicatedStorage:FindFirstChild("TeleportCountdownEvent")
if not teleportCountdownEvent then
teleportCountdownEvent = Instance.new("RemoteEvent")
teleportCountdownEvent.Name = "TeleportCountdownEvent"
teleportCountdownEvent.Parent = ReplicatedStorage
end
b) In your startCountdown(player) function, fire the event right away:
local function startCountdown(player)
-- Prevent double-starts
if playersOnPart[player] then return end
-- Tell the client to show the GUI and start counting down
teleportCountdownEvent:FireClient(player, "Start", TELEPORT_DELAY)
-- …then spawn your existing countdown thread…
local countdownThread = task.spawn(function()
task.wait(TELEPORT_DELAY)
if playersOnPart[player] == coroutine.running() then
playersOnPart[player] = nil
-- Optionally tell the client “stop” (though teleport unloads the client)
teleportCountdownEvent:FireClient(player, "Stop")
attemptTeleport(player)
end
end)
playersOnPart[player] = countdownThread
end
c) In your stopCountdown(player) function, fire the event to hide the GUI:
local function stopCountdown(player)
local thread = playersOnPart[player]
if thread then
task.cancel(thread)
playersOnPart[player] = nil
-- Tell the client to hide the GUI
teleportCountdownEvent:FireClient(player, "Stop")
end
end
That’s it on the server. Whenever a player steps on “teleport”, the server tells only that client to show or hide the countdown.
2) Client-side GUI & LocalScript
-
In StarterGui, insert a new ScreenGui (call it TeleportCountdownGui).
-
Inside it, insert a TextLabel and set its properties to taste (size, font, background, etc.).
• Name it CountdownLabel.
• Set Visible = false initially.
• Example size/position:
• Position: {0.5, -50},{0.2, 0, 0}
• Size: {0,100},{0,50}
• TextScaled = true
• TextColor = White, BackgroundTransparency = 0.5
-
Inside that same ScreenGui, insert a LocalScript and paste:
-- LocalScript inside TeleportCountdownGui
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local countdownEvent = ReplicatedStorage:WaitForChild("TeleportCountdownEvent")
local screenGui = script.Parent
local label = screenGui:WaitForChild("CountdownLabel")
local countdownConn = nil
local endTime = 0
-- Update function, runs each frame
local function onHeartbeat()
local timeLeft = endTime - tick()
if timeLeft <= 0 then
-- Done!
label.Visible = false
if countdownConn then
countdownConn:Disconnect()
countdownConn = nil
end
return
end
-- Display rounded up seconds
label.Text = "Teleporting in " .. math.ceil(timeLeft) .. "…"
end
-- Listen for server tells
countdownEvent.OnClientEvent:Connect(function(action, seconds)
if action == "Start" then
-- Show & start
endTime = tick() + seconds
label.Visible = true
-- Hook Heartbeat only once
if not countdownConn then
countdownConn = runService.Heartbeat:Connect(onHeartbeat)
end
elseif action == "Stop" then
-- Hide & cleanup
label.Visible = false
if countdownConn then
countdownConn:Disconnect()
countdownConn = nil
end
end
end)
How it works
• When the player steps on the “teleport” part, the server:
• Fires TeleportCountdownEvent with ("Start", 5).
• After 5 seconds (if they stay), fires ("Stop") and teleports them.
• If they leave early, fires ("Stop") immediately.
• The LocalScript:
• Upon Start, calculates endTime = now + seconds and shows the label.
• Hooks RunService.Heartbeat to update the text every frame.
• On Stop (or when countdown reaches zero), hides the label and disconnects.
And that’s it! Players will now see a neat countdown on their screen leading up to the teleport. 🚀