The timer i made in my game goes twice as fast when there are 2 players

  1. What do you want to achieve? Keep it simple and clear!
    i want to make a round/lobby system for my game

  2. What is the issue? Include screenshots / videos if possible!
    it worked at first but when my friend joined the timer went twice as fast and when a second friend joined it went thrice as fast

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    i tried fixing it myself but i dont know whats wrong. i looked for solutions in the hub but i didnt find anything

this is my script:

local players = game.Players
local rs = game.ReplicatedStorage
local values = rs.Values
local round = values.Round
local minutes = values.TimerM
local seconds = values.TimerS



players.PlayerAdded:Connect(function(plr)
	local function roundEnd()
		round.Value = false
		minutes.Value = 0
		seconds.Value = 40
			local chr = plr.Character or plr.CharacterAdded:Wait()
			local timertext = plr.PlayerGui:WaitForChild("RoundGUI"):WaitForChild("Timer")
			local text = plr.PlayerGui:WaitForChild("RoundGUI"):WaitForChild("TextLabel")
			chr.HumanoidRootPart.CFrame = workspace.LobbySpawn.Part.CFrame
			text.Text = "INTERMISSION"
			while round.Value == false do
				wait(1)
				if seconds.Value > 0 then
					seconds.Value -= 1
					if seconds.Value < 10 then
						timertext.Text = minutes.Value .. ":0" .. seconds.Value
					else
						timertext.Text = minutes.Value .. ":" .. seconds.Value
					end
				elseif minutes.Value > 0 and seconds.Value == 0 then
					minutes.Value -= 1
					seconds.Value = 59
					timertext.Text = minutes.Value .. ":" .. seconds.Value
				else
					print("round started")
	
					local function roundStart()
						round.Value = true	
						wait(1)
						local chr = plr.Character or plr.CharacterAdded:Wait()
						local timertext = plr.PlayerGui:WaitForChild("RoundGUI"):WaitForChild("Timer")
						local text = plr.PlayerGui:WaitForChild("RoundGUI"):WaitForChild("TextLabel")
						chr.HumanoidRootPart.CFrame = workspace.RoundSpawn.Part.CFrame
						text.Text = "ROUND IN PROGRESS"
						minutes.Value = 4
						seconds.Value = 0
						while round.Value == true do
							wait(1)
							if seconds.Value > 0 then
								seconds.Value -= 1
								if seconds.Value < 10 then
									timertext.Text = minutes.Value .. ":0" .. seconds.Value
								else
									timertext.Text = minutes.Value .. ":" .. seconds.Value
								end

							elseif minutes.Value > 0 and seconds.Value == 0 then
								minutes.Value -= 1
								seconds.Value = 59
								timertext.Text = minutes.Value .. ":" .. seconds.Value
							else
								print("round ended")
								roundEnd()
							end
						end
						
					end

					roundStart()
				end
			end
	end
	
	roundEnd()
end)

i don’t know what’s wrong and how to fix this problem. any help would be appreciated!

1 Like

you call roundstart and roundend every time a player is added and each playeradded is it’s own different thread

2 Likes

This should key off is there a player at all. Not if a player was added.

so how could i make it run without playeradded? it’s all in a server script

wait are you trying to start a new round start every time a new player joins on purpose?

i’m pretty sure you don’t need to make it run when a player joins, since the script only needs to run one time (when the server starts up from the first player joining)

also, i suggest moving the roundStart function out of the roundEnd function, since it’s causing unnecessary nesting in your code

Where is it parented at? If its parented somewhere like starter player or startercharacter the scripts duplicates so

Also dont make it when the players is added, make it happens when their is enough players / make it happen when the game starts if it makes you happy ig

okay, since i didn’t specify i put the playerAdded there so i can get the player to change the timer gui. Everyone’s telling me to not use it but is there a different way i could change the timer for all players?

wait for a player to join then call the functions, you can do like

local Players = game:GetService("Players");

function roundEnd()
 ...
end

function roundStart()
  ...
end

--// at the end of script
repeat task.wait() until #Players:GetPlayers() > 0;
roundEnd();

can’t you just use players.PlayerAdded and disconnect the event once it fires, instead of doing repeat task.wait()?

Don’t use players.PlayerAdded:Connect(function(plr) to start rounds. Everytime a new player joins the game it creates a new thread, so when a 2nd player joins the game the script will run the timer while loop for them too, so you’ll have the timer running 2 times. That’s why the timer goes twice as face when a second person joins.

1 Like

you could just use a for loop

for _, player in pairs(players:GetPlayers()) do
     --stuff goes here
end

ps I forgot RemoteEvent:FireAllClients() existed :sob: you should use that instead

I would use a remote event for gui changes and for each gui change in the server script do a :FireAllClients(GuiText) and have it listen for the remote event in the local script (gui script) and change the gui based on what you passed through the remote event

GUIs should never be controlled from the server. That is a client’s job. As @LuhLuaProgrammer said, have the server replicate timer state to all clients, then have each client update the UI locally. You can use a StringValue or a RemoteEvent

1 Like

in roblox studio the player will join before PlayerAdded is fired, so it’s easier to do this instead of making edge cases with RunService#IsStudio()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.