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

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

	local Hold = hum:FindFirstChild(“Animator”):LoadAnimation(script.BruceHold)
	Hold:Play()
      end
end

Perhaps this?

That means that your pairs yields infinitely or that there’s and error in your message that is not outputted.

If not that, then it’s probably because youre running the script instantly on the client, before the other player has loaded.

Reply to BendsSpace

It isn’t guaranteed to have an array of Player instances. If you just assume things that can change in the future your code will randomly break in the future. Even if it’s unlikely.

Why? The code shouldn’t care that there are non Player instances under the Players service. It doesn’t make sense for code that only loops through the players to apply an unspoken rule on the Players service.

GetChildren isn’t more consistent with other code, because in other code where GetChildren is used you get {Instance} not {Player}. So the there code is written in a way to accommodate that.

Unless you need to you shouldn’t create rules like this.

Update 17-1-2023

If you remove it right away when you don’t need it anymore, then the garbage collector will have less work to do. So you have smaller lag spikes when it does its thing. But yeah this isn’t really that much of an improvement.


Replying to the actual post, I’d say this is a good solution:

However I would probably replace task.wait(41) with Hold.Ended:Wait() (and Hold:Stop() with Hold:Destroy()), unless 41 isn’t the length of the animation.

Well, it looks like we already have enough content in previous answers, but there’s always room for more :>

To begin with, we must understand a little about what is causing this problem

This is not a simple question, after all, there may be several conflicts with the codes you made available, including the attempt to call a Character of a player not yet correctly added, the player was not present in the game at the time the code was executed , each player has a different RigType in which they may not be able to execute the animation because it is a different RigType, the Humanoid Animator was not added correctly, thus ignoring the LoadAnimation call, among other cases.

Below I will make available a code adapted to correct some of the problems, but as for problems such as the RigType of the players or the time of entry of each one within the current game, this will have to be solved by yourself.

--// Services
local Players = game:GetService('Players')

--// Script
local Animation = script:FindFirstChild('BruceHold')

--// Void
for _, player in next, Players:GetPlayers() do
    task.spawn(function()
        local character = player.Character or player.CharacterAdded:Wait()
        local humanoid = character:WaitForChild('Humanoid')
        local animator = humanoid:FindFirstChild('Animator') or Instance.new('Animator', humanoid)
        
        local animationTrack = animator:LoadAnimation(Animation)
        animationTrack:Play()
    end)
end

This code is just an example of the most assertive implementation of your code, but it should be tested and improved depending on what you want to do.

I said this first:

Just briefly checking what you wrote, you didn’t use task.wait and you also didn’t use :FindFirstChild to ensure that the humanoid exists to avoid an infinite yield.

task.spawn doesnt yield code, i hope you know that, plus if the Humanoid Doesnt exist, the code wont do anything.

Sorry but I didn’t realize you had task.spawn.

Either way though, in your code, if a player doesn’t have a character then it will error, which isn’t ideal. Also, you should use task.wait(41) instead of wait(41)