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

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.

Thats not what I meant, i meant this:

for _, v in pairs(game.Players:GetChildren()) do
    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)
end

isn’t task.spawn deprecated? [ char limit]

No?

The Regular spawn() is deprecated, task.spawn() is the newer variant of the deprecated spawn()

task.spawn

isn’t there coroutines now though?

yes, but task does the exact same thing, run along side the current thread, it is also faster as it runs immediately.

task.spawn(function()

end)

local co = coroutine.create(function()

end)

coroutine.resume(co)

Read Documentation tho

but for more advanced cases it’s recommended you familiarize yourself with the coroutine library.

coroutine.resume(coroutine.create(function()

end)

works too

Yes, but there is really only one difference:

In this case, it would be better to use task.spawn()

Maybe try using game.Players:GetPlayers()) ?

Try this:

for i, player in ipairs(game.Players:GetPlayers()) do
	task.spawn(function()
		local character = player.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")

		if humanoid then
			local Hold = humanoid.Animator:LoadAnimation(script.BruceHold)
			Hold:Play()
			
			task.wait(41)
			Hold:Stop()
		end
	end)
end

Players:GetChildren() or Players:GetPlayers() do the same thing and are equally good. As far as future proofing, it’s virtually guaranteed the behavior of either is never going to change.

Coroutines have always been a thing.

This is the same thing as game.Players:GetChildren().


Katrist’s code is very likely the solution (the OP’s and other user’s code should check if the character exists before trying to get the humanoid in addition to running some stuff in parallel with task library functions).

1 Like

why you use GetChildren to get players instance, i suggest to use :GetPlayers() instead. because GetPlayers only get a player inside players childrens

2 Likes

While this is true, you should still use Players:GetPlayers() as that ensures what you’re checking is an actual player, unlike Players:GetChildren() which gets anything.

1 Like