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.
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.
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
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.
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:
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)
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.
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?
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)
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)