I have a game with a timer GUI at the top. However, the timer doesn’t always work. It works for a little bit, then it resets and just says “label”. I was wondering if this was because the player wasn’t fully loaded in, so how would I make sure the player is fully loaded in? Here are the scripts:
server:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local timeCounter = ReplicatedStorage.Time
local timeUntil = ReplicatedStorage.TimeUntil
local gravel = ReplicatedStorage.Gravel
local runners = ReplicatedStorage.Runners
local lobby = ReplicatedStorage.Lobby
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local lobbyTeam = Teams.Lobby
local gravelTeam = Teams.Gravel
local runnersTeam = Teams.Runners
local lobbySpawn = game.Workspace.Lobby.LobbySpawn
local gravelWait = game.Workspace.GravelWaiting.GravelWaiting
local runnerSpawn = game.Workspace.Playground.RunnerSpawn
local gravelSpawn = game.Workspace.Playground.GravelSpawn
while true do
--intermission
timeUntil.Value = "Intermission"
for seconds = 20, 0, -1 do
timeCounter.Value = seconds
task.wait(1)
end
if #Players:GetPlayers() <= 1 then
while #Players:GetPlayers() <= 1 do
timeUntil.Value = "Not enough players."
task.wait(3)
timeUntil.Value = "Intermission"
for seconds = 20, 0, -1 do
timeCounter.Value = seconds
task.wait(1)
end
end
end
--set gravel
local gravelPlayer = Players:GetPlayers()[math.random(1, #Players:GetPlayers())]
gravel:FireClient(gravelPlayer)
gravelPlayer.Team = gravelTeam
print(gravelPlayer.Name .." is now gravel")
local gravelChar = gravelPlayer.Character
if not gravelChar or not gravelChar.Parent then
gravelPlayer.CharacterAdded:Wait()
end
local gravelHrp = gravelChar:WaitForChild("HumanoidRootPart")
local gravelHumanoid = gravelChar:WaitForChild("Humanoid")
gravelHrp.CFrame = gravelWait.CFrame
gravelHumanoid.WalkSpeed = 20
-- send players to game
for i, plr in pairs(lobbyTeam:GetPlayers()) do
runners:FireClient(plr)
plr.Team = runnersTeam
local char = plr.Character
if not char or not char.Parent then
char.CharacterAdded:Wait()
end
local hrp = char:WaitForChild("HumanoidRootPart")
hrp.CFrame = runnerSpawn.CFrame
end
-- headstart
timeUntil.Value = "Gravel spawns in:"
for seconds = 30, 0, -1 do
timeCounter.Value = seconds
task.wait(1)
end
-- start game(this loop is where the break has to be)
gravelHrp.CFrame = gravelSpawn.CFrame
timeUntil.Value = "Game in progress."
for seconds = 300, 0, -1 do
timeCounter.Value = seconds
if #runnersTeam:GetPlayers() == 0 then
break
end
task.wait(1)
end
-- game over/send to lobby
timeUntil.Value = "Game Over"
task.wait(3)
if #runnersTeam:GetPlayers() >= 1 then
timeUntil.Value = "Runners Win!"
else
timeUntil.Value = "Gravel Wins!"
end
gravelHumanoid.WalkSpeed = 16
for i, plr in pairs(Players:GetPlayers()) do
plr.Team = lobbyTeam
lobby:FireClient(plr)
local char = plr.Character
if not char or not char.Parent then
char.CharacterAdded:Wait()
end
local hrp = char:WaitForChild("HumanoidRootPart")
hrp.CFrame = lobbySpawn.CFrame
end
task.wait(3)
end
client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local timeCounter = ReplicatedStorage.Time
local timeUntil = ReplicatedStorage.TimeUntil
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local playerGui = localPlayer:WaitForChild("PlayerGui")
local timerScreen = playerGui:WaitForChild("Timer")
local timerFrame = timerScreen.Timer
local timer = timerFrame.Timer
local timeUntilText = timerFrame.TimeUntil
timer.Text = tostring(timeCounter.Value)
timeUntilText.Text = tostring(timeUntil.Value)
timeCounter.Changed:Connect(function(newValue)
timer.Text = tostring(newValue)
end)
timeUntil.Changed:Connect(function(newString)
timeUntilText.Text = tostring(newString)
end)
So how do I fix this?