game.Players:GetPlayers() only returning one player

Im making a swordfighting game with rounds, and the way it works is at the beginning of each round a vault opens up and the players automatically get moved to fall through it into the map. My issue is that it only affects one player, instead of all the players in the game. Why is this? Here is my code. (This is not the entire script, just a piece of it.)

while true do 
	wait(30)
	local randommap = maps[math.random(1, #maps)]:Clone()
	randommap.Parent = game.Workspace
	local animator = require(game.Workspace.Vault.Animator)
	wait(2)
	local children = game.Players:GetChildren()
	for _, player in pairs(game.Players:GetPlayers()) do
		print("player")
			game.ReplicatedStorage.cam:FireAllClients()
			wait(0.5)
			player.Character.Humanoid.WalkSpeed = 25
			player.Character.Humanoid:MoveTo(randommap.Spawn.Position, randommap.Spawn)
			animator.OpenVault:Play()
			player.Character.Humanoid.MoveToFinished:Wait()
			player.Character.Humanoid.WalkSpeed = 16
			game.ReplicatedStorage.cam2:FireAllClients()
			player.TeamColor = BrickColor.new("Dark blue")
			local sword = ss.swords[tostring(player.Inventory.Equipped.Value)]:Clone()
			wait(10)
			sword.Parent = player.Character
	end
1 Like

You might want to look into task.spawn()

another thread:

for your code, you’ll want to do something like:

		task.spawn(function()
			print("player")
			game.ReplicatedStorage.cam:FireAllClients()
			wait(0.5)
			player.Character.Humanoid.WalkSpeed = 25
			player.Character.Humanoid:MoveTo(randommap.Spawn.Position, randommap.Spawn)
			animator.OpenVault:Play()
			player.Character.Humanoid.MoveToFinished:Wait()
			player.Character.Humanoid.WalkSpeed = 16
			game.ReplicatedStorage.cam2:FireAllClients()
			player.TeamColor = BrickColor.new("Dark blue")
			local sword = ss.swords[tostring(player.Inventory.Equipped.Value)]:Clone()
			wait(10)
			sword.Parent = player.Character			
		end)

some other notes on your code:
why do game.ReplicatedStorage.cam:FireAllClients() for every player in the game?

how on earth would this fix my script? the issue isn’t the loop, it has something to do with how the getplayers is called

Does player only print once? If so it might be because you are firing to multiple clients.

-- n = number of players
-- FireAllClients fires to all clients n number of times

as for the remoteevent, i accidentally put it into the loop

Yes, player only prints once charscharschars

player.Character.Humanoid.MoveToFinished:Wait()

is yielding the loop until the character is done moving

if you want all of the players to move at once thats where you’d use task.spawn().

No its because you are yielding the for loop with the wait(10) your code is wrong there seems to be yielding that you need to fix

getplayers will relay all available players.

1 Like

Oh, thank you. I understand charschars

exactly what @Juicy_Fruit said.

:GetPlayers() is working correctly, but it can’t go to the next player in the loop without waiting the 10 seconds.

Once again, look into task.spawn()

Thank you, I implemented task.spawn and it worked perfectly!