Why is this animation script not working

I’m trying to use a script (located in StarterPlayerScripts) to animate all Dummys in the Dummys folder. My problem is that it only animates a random one. It should be animating all of them. Thank you for help in advance

Script:

local D = workspace.Dummys:GetChildren()

for i,v in pairs(D) do

local wave = true

local hum = v:WaitForChild("Humanoid")
local loaded = hum:LoadAnimation(script.Idle)
loaded.Looped = true
loaded:Play()

while wave == true do
	wait(math.random(3,6))
	local loaded = hum:LoadAnimation(script.Wave)
	loaded.Priority = Enum.AnimationPriority.Action2
	loaded.Looped = false
	loaded:Play()
	local loaded = hum:LoadAnimation(script.Idle)
	loaded.Looped = true
	loaded:Play()
end
	
end

Result:
Screenshot_5

Explorer:
image

The issue with the code is that it is only animating one dummy at a time because the “while” loop is causing it to get stuck in the animation sequence for one dummy. Here’s a modified version of the code that should animate all the Dummys in the folder:

local DummysFolder = workspace.Dummys
local animations = {}

-- Load animations
for _, animation in pairs(script:GetChildren()) do
    animations[animation.Name] = animation
end

-- Animate Dummys
for _, Dummy in pairs(DummysFolder:GetChildren()) do
    local hum = Dummy:WaitForChild("Humanoid")
    local wave = true
    local loaded = hum:LoadAnimation(animations.Idle)
    loaded.Looped = true
    loaded:Play()

    while wave do
        wait(math.random(3, 6))
        local waveAnim = hum:LoadAnimation(animations.Wave)
        waveAnim.Priority = Enum.AnimationPriority.Action2
        waveAnim.Looped = false
        waveAnim:Play()
        loaded:Stop()
        waveAnim.Stopped:Wait()
        loaded:Play()
    end
end

What this does is first loads all the animations into a table and then animates each Dummy in the folder. The “while” loop has been modified to play the wave animation, stop the idle animation, wait for the wave animation to finish, and then play the idle animation again. This way, it will animate each dummy in turn, rather than getting stuck in the animation sequence for one dummy.

Hey, thank you for your help. I tried that in studio and still comes with the same result as my script
image

I see. In that case, it’s possible that the issue lies with the way that the while loop is structured.

Right now, the while loop continues to execute until wave is set to false, which never happens in the code you provided. This means that the code will continue to play the wave animation on the current dummy indefinitely.

To fix this, you can modify the while loop to only execute once and then set wave to false once the wave animation has finished playing. Here’s how you can modify the code to do this:

local D = workspace.Dummys:GetChildren()

for i,v in pairs(D) do

    local wave = true

    local hum = v:WaitForChild("Humanoid")
    local loaded = hum:LoadAnimation(script.Idle)
    loaded.Looped = true
    loaded:Play()

    while wave do
        wait(math.random(3,6))
        local loaded = hum:LoadAnimation(script.Wave)
        loaded.Priority = Enum.AnimationPriority.Action2
        loaded.Looped = false
        loaded:Play()
        loaded.Stopped:Wait() -- wait for wave animation to finish playing
        loaded = hum:LoadAnimation(script.Idle)
        loaded.Looped = true
        loaded:Play()
        wave = false -- set wave to false to exit loop
    end

end

In this modified code, the wave variable is initially set to true, just like in your original code. The while loop now executes once and then sets wave to false once the wave animation has finished playing. The code then exits the while loop and continues to the next iteration of the for loop, which will play the animations on the next dummy in the Dummys folder.

Unfortunately the problem still exists! I tested it but still the animation with only apply on one random dummy

Heyylooo!

I might of fixed your code here, all you had to do was use a coroutine.resume(coroutine.create(function), which performs multiple tasks and will help with what you are trying to achieve.

I did use holis code, but i just fixed it up a bit.

local wave = true

task.spawn(function()
	for _, v in ipairs(workspace.Dummys:GetChildren()) do
		
		coroutine.resume(coroutine.create(function()
			local hum = v:WaitForChild("Humanoid")
			local loaded = hum:LoadAnimation(script.AnimTrack1)

			loaded.Looped = true
			loaded:Play()

			while wave do
				wait(math.random(3,6))
				loaded.Priority = Enum.AnimationPriority.Action2
				loaded.Looped = false
				loaded:Play()
				loaded.Stopped:Wait() -- wait for wave animation to finish playing
				loaded = hum:LoadAnimation(script.AnimTrack2)
				loaded.Looped = true
				loaded:Play()
				wave = false -- set wave to false to exit loop
			end
		end))

	end
end)

This should be an easy fix. You need to change this line to local loaded = hum:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("Idle"))


Humanoid:LoadAnimation is deprecated in favor of Humanoid.Animator:LoadAnimation


As well as you need to fix the loaded animation in the while wave loop (both lines), and I would also suggest changing the line waveAnim.Priority = Enum.AnimationPriority.Action2 to simply just use Action. In my personal experience, it causes some issues using Action2, Action3 or Action4. But again, just my personal experience.

If changing the lines that use hum:LoadAnimation to hum.Animator:LoadAnimation does not help, please let me know.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.