For _, v in pairs(game.Players:GetChildren()) do only executing on one user

Hi, I have the following code inside a server script. I intended that it would grab all the players inside the game, and run the animation on each player.

However, the animation is only playing on one user inside the game.

for _, v in pairs(game.Players:GetChildren()) do
	local char = v.Character
	local hum = char:WaitForChild("Humanoid")

	local Hold = hum:LoadAnimation(script.BruceHold)
	Hold:Play()
end

Any guidance on how to fix this problem would be very appreciated.

Is there an error in the console?

No errors were printed, I am very confused

This is irrelevant to your post, but I do suggest you use the game. Players: GetPlayers(), It’s just a better way to grab all players. I don’t see an issue with the code however, I’m gonna try it on Roblox studio and see what I can do to solve this.

3 Likes

How many players are in the Game?

I’m pretty sure he team tested it, to come to the argument that the animation is only playing for one player.

no, the code should work, the loop is iterating through all Players, which means it will fire code per number of players in the game, if its stopping at just one player, its likely there is a piece of code not provided or the game cannot find the Humanoid as there is a WaitForChild, or, there is only one player

There was two users in-game at the time we tested, no errors were printed yet the animation was only playing on one user.

Where exactly is the script placed? In the workspace or somewhere within a player’s character? Maybe where the animation instance actually is might affect how it loads for other players.

The script is located inside of workspace currently

both Players:GetChildren() and Players:GetPlayers() do the exact same thing, there is no difference

1 Like

Okay so, I tested it with 2 players, and I made a few adjustments, I think its best if you wait for the player and their character to load in before playing the animation as you can see here:
Screenshot 2023-01-15 at 2.43.53 PM
As you can see both the characters are playing the animation, the reason your code might have not worked is that the for loop started before the players completely loaded in, here is the finished code hope in helped.

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
for _, v in pairs(game.Players:GetChildren()) do
local char = v.Character
local hum = char:WaitForChild(“Humanoid”)

local Hold = hum:LoadAnimation(script.BruceHold)
Hold:Play()
end
end)
end)

1 Like

I wrote this off the dev forum, so if there is a bug just tell me.

Thank you so much for the help, however, this script is enabled midway through a game so I am unsure if this is a loading issue?

It is, but I think it’s better to use:GetPlayers instead because Roblox keeps updating and they might put some objects in the players, I will agree, both of them are the same.

Doesn’t change anything,

Both of them return the Player Instance, it doesnt check if there is anything inside

2 Likes

I think I’ve found the cause of my issue.

I had a wait attached to one of the other scripts I was using to control animation.

for _, v in pairs(game.Players:GetChildren()) do
	local char = v.Character
	local hum = char:WaitForChild("Humanoid")

	local Hold = hum:LoadAnimation(script.BruceHold)
	Hold:Play()


	wait(41)
	Hold:Stop()

this then stops the animation after a period, is the script waiting 41 seconds before applying the animation to the next player?

Yep.


The Loop is waiting 41 seconds before moving on, after that, it will skip to the next iteration (next Player if you will), i recommend using task.spawn() or task.defer()

task.spawn(function()
	local char = v.Character
	local hum = char:WaitForChild("Humanoid")

	local Hold = hum:LoadAnimation(script.BruceHold)
	Hold:Play()


	wait(41)
	Hold:Stop()
end)

you may also use task.delay() for the waiting:

task.delay(41, function()
    Hold:Stop()
end)

change

local char = v.Character

to

local char = v.Character or v.CharacterAdded:Wait()

(I assume this is being ran on the server)

task.spawn(function()
for _, v in pairs(game.Players:GetChildren()) do
	local char = v.Character
	local hum = char:WaitForChild("Humanoid")

	local Hold = hum:LoadAnimation(script.BruceHold)
	Hold:Play()


	wait(41)
	Hold:Stop()
end
end)

I’ve tried this, and the issue persists.