Round system breaks frequently

so i have this round system in place and when i test it with a local server, it just gives player2 the win sometimes. any fixes??

script:

ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")
teams = game:GetService("Teams")
DataStoreService = game:GetService("DataStoreService")
CashDataStore = DataStoreService:GetDataStore("cashDataStore")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
RoundStatus = ReplicatedStorage:WaitForChild('RoundStatus')

local Survivors = {}

while true do
	if #game.Players:GetPlayers() >= 2 then
		--Intermission

		local Countdown = 5

		repeat wait(1)
			Countdown = Countdown - 1

			RoundStatus.Value = 'Intermission : '..Countdown
		until Countdown <= 0

		--Choose the map.

		RoundStatus.Value = 'Choosing minigame..'

		local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
		local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()

		wait(3)
		
		RoundStatus.Value = ChosenMap.Name.." has been chosen!"

		wait(3)

		ChosenMap.Parent = workspace
		RoundStatus.Value = 'Teleporting players..'

		wait(1)

		--teleport the players



		local Players = game.Players:GetPlayers()

		for _, Player in pairs(game.Players:GetPlayers())do
			if Player.Character and Player.Character:FindFirstChild('Humanoid') then
				local RandomSpawn = Spawns[math.random(1, #Spawns)]
				Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				Player.TeamColor = BrickColor.new("Persimmon")
				Player.Character.Humanoid.MaxHealth = 100
				Player.Character.Humanoid.Health = 100
				Player.Character.Humanoid.WalkSpeed = 16
				Survivors[Players] = true
			end
		end

		local Countdown = 180

		for i = Countdown, 0, -1 do
			RoundStatus.Value = "Game In Progress: "..i
			
			wait(1)

			local Survivor = game.Teams.Playing
			if #Survivor:GetPlayers() == 1 then
				for i,v in pairs(game.Teams["Playing"]:GetPlayers()) do
					RoundStatus.Value = v.Name.. " has won!"
					v.leaderstats.Cash.Value = v.leaderstats.Cash.Value + 5
					
					for _, Player in pairs(game.Players:GetChildren())do
						if Player.Character and Player.Character:FindFirstChild('Humanoid') then
							Player.Character.Humanoid:TakeDamage(9999999999)
						end
					end
					
					wait(5)

					ChosenMap:Destroy()

					RoundStatus.Value = 'Round over!'

					wait(1)
					
				end
				break
			end

			wait(1)
		end
	else
		local dots = "..."

		repeat
			for i = 1,3 do
				RoundStatus.Value = "Waiting For Players"..string.sub(dots, 1, i)
				wait(1)
			end
		until #game.Players:GetPlayers() >= 2
	end

end

You should be using for loops, you can read more on it here.

1 Like

Also if I’m not mistaken there’s an error here

what is the error there? i remade my round script with that part and its still breaking

Try using pcall

local suc , er = pcall(function()

   if suc then
 -- code here
  elseif er then
    warn(er)
end

end)

For loops for what?

@imAlex_30
He said that it will sometimes give player 2 the win.

but theres no error or anything in console so it would just have the same issue

i converted everything to for loops and it still doesnt work so ur right

Maybe player 2 is winning thats why win is going to player 2?

no it happens as soon as it teleports so im assuming thats where the issue is

for some reason it doesnt give the team to player1, resulting in player2 to win

If you add prints around you can see where and when it goes into something, but I feel that it could be starting before everyone is in there.

no it just does not give the team to the first player. i tested it with 3 players and it just doesnt give the team to player1

Want to change this pairs to ipairs I don’t think it will fix it but it’s worth a shot.

I see,

in loops you put Player

and in table you put Survivors[Players] = true

Players. Try removing s?

Also, just looked at it again, it could be that the player doesn’t have a character yet so it doesn’t add them to the team.

And no no, I quoted your code, I was saying to do ipairs() instead of pairs(). But I think the problem is that the player doesn’t have a character yet.

i removed the s and it just fixes it for one round then continues to break

so do i make it wait for child

I would just remove the part that checks if they have a character so it will put them on the team still.

Or you can just make it wait a little longer after there are enough players to go.

image

1 Like