One player gets their items and gets teleported, but nothing happens to the others

I’m working on this main script for a game, and this part of the code has an issue I can’t seem to fix.

for i,v in pairs(game.Players:GetChildren()) do
	if v.role.Value == "Guard" then
		v.Character:MoveTo(game.Workspace.GuardSpawn.Position + Vector3.new(0, 4, 0))
		for i,x in pairs(game.ServerStorage.TeamTools.Guard:GetChildren()) do
			x:Clone().Parent = v.Backpack
		end
	end
			
			
	if v.role.Value == "Robber" then
		v.Character:MoveTo(game.Workspace.RobberSpawn.Position + Vector3.new(0, 4, 0))
		for i,x in pairs(game.ServerStorage.TeamTools.Robber:GetChildren()) do
			x:Clone().Parent = v.Backpack
		end
	end
			
			
	if v.role.Value == "Worker" then
		--v.Character:MoveTo(game.Workspace.WorkerSpawn.Position + Vector3.new(0, 4, 0))
		for i,x in pairs(game.ServerStorage.TeamTools.Worker:GetChildren()) do
			x:Clone().Parent = v.Backpack
		end
	end
			
			
	if v.role.Value == "Civillian" then
		--v.Character:MoveTo(game.Workspace.CivillianSpawn.Position + Vector3.new(0, 4, 0))
		for i,x in pairs(game.ServerStorage.TeamTools.Civillian:GetChildren()) do
			x:Clone().Parent = v.Backpack
		end
	end
			
	if v.role.Value == "" or nil then
		warn(v.Name.. " has no role")
	end
		
	game.ReplicatedStorage.Remotes.RoleInfo:FireAllClients()
	wait(50000)
end

The first player the script loops through will have their items and be teleported correctly, but any other players after them never receive their items or get teleported.

Nothing of interest is in the output, and I’m stumped. Does anyone know why this is happening?

The other players do have their roles and receive the RoleInfo event correctly.

1 Like

The reason why this happens is because of the wait(50000) inside of the for i,v loop, meaning the code waits 50,000 seconds for each player to get teleported and gain their tools.

3 Likes

You can fix this by removing the wait function, or placing it out side of the for loop.

Another thing you should probably take out of the loop is the :FireAllClients() function, as it’ll fire to all clients for every player, and I assume that’s not what you want.

2 Likes

i thought i put that outside of the loop

thanks tho :smile:

2 Likes

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