The timer isn’t really “randomly” resetting – every time any player joins (or their Heartbeat loop fires) the single, shared BillboardGui is overwritten with that player’s remaining-time value.
So, from each player’s point of view, the counter keeps jumping back and forth.
Key problem
• timerLabel is a global object in the workspace. All players write to the same label.
Solution in a nutshell
- Keep the BillboardGui in the workspace only as a template.
- When a player joins, clone that template into the player’s
PlayerGui (or onto their character’s head).
- Update the cloned label instead of the shared one.
- (Minor) Use
os.clock() instead of tick() so system-time corrections can’t mess with you.
Complete, fixed server script
--[[ Playtime / Invitation reward script
Each player gets an independent timer UI.
]]--
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local GravityCoil = ServerStorage:WaitForChild("GravityCoil")
local MinutesRequired = 15 -- minutes of playtime
local FriendsRequired = 5 -- invites required
local part = workspace:WaitForChild("PlaytimeGiverPart")
local templateBillboard = part:WaitForChild("BillboardGui") -- **template only**
templateBillboard.Enabled = false -- hide the template in-game
--------------------------------------------------------------------
-- Utility
--------------------------------------------------------------------
local function hasInvitedEnoughFriends(player : Player) : boolean
local stats = player:FindFirstChild("leaderstats")
if not stats then return false end
local invites = stats:FindFirstChild("FriendsInvited")
return invites and invites.Value >= FriendsRequired
end
local function giveGravityCoil(player : Player)
if player.Backpack:FindFirstChild("GravityCoil")
or (player.Character and player.Character:FindFirstChild("GravityCoil")) then
return
end
GravityCoil:Clone().Parent = player.Backpack
end
--------------------------------------------------------------------
-- Per–player UI
--------------------------------------------------------------------
local function createGuiFor(player : Player) : TextLabel
-- Clone the template into the player’s PlayerGui
local gui = templateBillboard:Clone()
gui.Enabled = true
gui.Name = "PlaytimeGui"
gui.ResetOnSpawn = false -- keep GUI on respawn
-- We still want it to hover above the in-world part:
gui.Adornee = part
gui.Parent = player:WaitForChild("PlayerGui")
-- Small cosmetic setup
local infoLabel = gui:WaitForChild("InfoLabel")
local timerLabel = gui:WaitForChild("TimerLabel")
infoLabel.Text = string.format(
"Play for %d minutes or invite %d friends for a surprise!",
MinutesRequired, FriendsRequired
)
return timerLabel
end
--------------------------------------------------------------------
-- Main logic
--------------------------------------------------------------------
Players.PlayerAdded:Connect(function(player : Player)
local timerLabel = createGuiFor(player)
local joinedTime = os.clock() -- monotonic, not affected by system clock
local rewarded = false
-- Reward immediately if they already have enough invites
if hasInvitedEnoughFriends(player) then
giveGravityCoil(player)
rewarded = true
timerLabel.Text = "You got the Gravity Coil! 🎉"
end
-- Heartbeat timer loop
local hbConn;
hbConn = RunService.Heartbeat:Connect(function()
if not player.Parent then -- player left
hbConn:Disconnect(); return
end
if rewarded then -- already received reward
hbConn:Disconnect(); return
end
local elapsed = os.clock() - joinedTime
local remain = math.max(0, MinutesRequired * 60 - elapsed)
local mins = math.floor(remain / 60)
local secs = math.floor(remain % 60)
timerLabel.Text = string.format("Time Left: %02d:%02d", mins, secs)
if remain <= 0 then
giveGravityCoil(player)
rewarded = true
timerLabel.Text = "You got the Gravity Coil! 🎉"
hbConn:Disconnect()
end
end)
end)