Issue with NPC Animation Script

So I turned my Waving NPC Animation scripts into just one script because the many loops were lagging the game.

Whenever I run the game, the NPCs are stuck waving mid-way probably because it’s repeating the function every .1 second.

I’ve tried putting coroutine wraps for the functions and taking out the wait but nothing works. Here is my current code:

local wave = script:WaitForChild('WavingID')
local idle = script:WaitForChild('IdleID')

task.wait(2)

local npcs = workspace:WaitForChild("WavingNPCs")

local function anim(hum)
	local dance = hum:LoadAnimation(wave)
	local dance2 = hum:LoadAnimation(idle)
	
	dance:Play()
	task.wait(1.6)
	dance:Stop()
	local random = math.random(3.9,7.9)
	dance2:Play()
	task.wait(random)
	dance2:Stop()
end

while true do
	for _,v in pairs(npcs:GetChildren()) do
		coroutine.wrap(anim)(v.NPC)
	end
	task.wait(.1)
end

Please tell me if you know how to fix this.

local wave = script:WaitForChild('WavingID')
local idle = script:WaitForChild('IdleID')

task.wait(2)

local npcs = workspace:WaitForChild("WavingNPCs")

local function anim(hum)
	local dance = hum:LoadAnimation(wave)
	local dance2 = hum:LoadAnimation(idle)

    if dance.IsPlaying then return end
    if dance2.IsPlaying then return end
	
	dance:Play()
	task.wait(1.6)
	dance:Stop()
	local random = math.random(3.9,7.9)
	dance2:Play()
	task.wait(random)
	dance2:Stop()
end

while task.wait(.1) do
	for _,v in pairs(npcs:GetChildren()) do
		coroutine.wrap(anim)(v.NPC)
	end
end

I replaced it with your code but the issue still persists.

its because you wrap the function in the coroutine use a debounce in the animation function like

local debounceCache = {}

local function anim(hum)
   if(debounceCache[hum] == nil) then debounceCache[hum] = true end;
   if(not debounceCache[hum]) then return end;
  -- anim function stuff
  debounceCache[hum] = false;
end

It worked but it’s playing the animation slowly. and it’s not repeating.

load the animations outside the anim function and try animation.Stopped:Wait() instead of the task.wait(), I think I messed up the debounce, there should be

debounceCache[hum] = false;

before the anim function stuff, and

debounceCache[hum] = true;

after the anim function stuff.

As for the slow animation playback, maybe try changing the Animation PlaybackSpeed? Not too sure about whether it’s stuttering or just slow.

Whenever I load the animations outside the function I get an error that says “Attempt to index nil with ‘Play’”, and yes, I carried over the animations to the function in the parameters. Also I meant stuttering for the animation.

Here is my code now:

local wave = script:WaitForChild('WavingID')
local idle = script:WaitForChild('IdleID')

task.wait(2)

local npcs = workspace:WaitForChild("WavingNPCs")

local debounceCache = {}

local function anim(hum)
	if (debounceCache[hum] == nil) then debounceCache[hum] = true end
	if (not debounceCache[hum]) then return end
	debounceCache[hum] = false
	
	local dance = hum:LoadAnimation(wave)
	local dance2 = hum:LoadAnimation(idle)
	
	dance:Play()
	dance.Stopped:Wait()
	dance:Stop()
	local random = math.random(3.9,7.9)
	dance2:Play()
	dance2.Stopped:Wait()
	dance2:Stop()
	
	debounceCache[hum] = true
end

while true do
	for _,v in pairs(npcs:GetChildren()) do
		local human = v:WaitForChild("NPC")
		coroutine.wrap(anim)(human)
	end
	task.wait(.1)
end

The NPC is constantly waving.

try change the Animation.Looped to false and remove the :Stop() parts

1 Like

It finally works now, thank you.