The top bar countdown shows up wrong on client

I’m trying to make my top bar function right, but the timer only visibly counts down live on the server’s screen, how do I fix this?
It still counts down like it’s supposed to on server, but on client, you have to reset to see the timer countdown
Client
Server

local StatusBar = game.StarterGui.TopFrame.Bar
local GameStatus = "Intermission"
local Time = 31

function GetPlayerCount()
	Players = #game.Players:GetPlayers()
end

wait(1)

while true do
	if GameStatus == "InGame" then
		GetPlayerCount()
		if Players == 1 or Time == 0 then
			GameStatus = "Intermission"
			Time = 31
		elseif Players >= 2 or Time > 0 then
			Time = Time - 1
			print(Players)
			print(Time)
			print(GameStatus)
			StatusBar.Text = Time
			wait(1)
		end
	elseif GameStatus == "Intermission" then
		GetPlayerCount()
			if Players >= 2 or Time > 0 then
				Time = Time - 1
				print(Players)
				print(Time)
				print(GameStatus)
				StatusBar.Text = "INTERMISSION ".. Time
				wait(1)
			elseif Players == 50 or Time == 0 then
				GameStatus = "InGame"
				Time = 300
		end
	end
end

You’re modifying the contents of StarterGui, which is not what you want.
All instances named Starter... are merely reference containers and their contents are copied to other instances. If you modify contents inside of StarterGui, it will only affect players who load the Gui past the changes.

Instead, you’ll want to modify the actual client’s Gui inside Player.PlayerGui. It’s best to do so via a LocalScript. I suggest reading these articles to get a better understanding and know-how:
https://developer.roblox.com/en-us/api-reference/class/PlayerGui
https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

To add to this, the timer itself should be handled on the server, and you should replicate that to all of the clients every second.

Psuedocode:

-- server
for Time = 30, 1, -1 do
     RemoteEvent:FireAllClients(Time) -- update clients
     wait(1)
end
-- client
RemoteEvent.onClientEvent:Connect(function(Time)
     StatusBar.Text = Time
end

Or you could do :FireAllClients() once and have the loop inside of the event function instead of firing the remote every second.

You could basically change everything to be on the server instead of firing a remote every second for every client in the game.

If a player joins after the event is fired, though, they won’t be able to see the time until the next event is fired. It’s generally better to do what @MayorGnarwhal did because they made it so that there is a maximum 0.99 second delay for the time, but that amount of time is usually taken up by the game’s loading screen anyway. The time will stay updated to all players every single second, so no matter what happens they are all staying up to date with the events.

You could client replicate it with a Value! Try this:

Place a Value in your ReplicatedStorage and a localscript inside your GUI.

--[[SERVER SCRIPT]]--

local val = game.ReplicatedStorage:WaitForChild("Value")

for count = 1, 31
	local count = 31
	count -= count
	val.Value = "Intermission: " .. Time
	wait(1)
end

--[[LOCAL SCRIPT]]--

local val = game.ReplicatedStorage:WaitForChild("Value")

val.Changed:Connect(function()
	script.Parent.Text = val.Value
end)

Hope this helped!

Meh… I was gonna type that instead, but yours has a mistake.

This:

Also, use WaitForChild() to get the Value. Else, the script might check if the value is there too fast and the whole thread might get terminated.

1 Like

You should add a StringValue to the replicated storage and have the text that the Gui should have as the value of the stringvalue. (You can set this at the server.)

You could also put a bool value inside that string where it checks if its ingame or not.

So you do a local script inside the gui you want to do the changes and do Text = StringValue.Value

1 Like

Oh, my mistake. Thanks for catching that!

2 Likes

Thanks for the replies! I will take these in to account, and do my own research on this myself!

1 Like