Animations not playing even if loaded

I have a system in my game where randomly chosen NPCs (with the same animations) will walk in to your restaurant and order something. Before the main customer handler script begins, it will load all animations on to the animator instances of all customers. For some odd reason, these animations never play.

Segment of code where it loads animations

local walktrack
local thinktrack
local paytrack
local holdtrack

for i, v in pairs(game.ServerStorage.Customers:GetChildren()) do
	task.wait(1)
	local Humanoid = v:WaitForChild("Humanoid")
	walktrack = Humanoid.Animator:LoadAnimation(script.Walk)
	thinktrack = Humanoid.Animator:LoadAnimation(script.Think)
	paytrack = Humanoid.Animator:LoadAnimation(script.Pay)
	holdtrack = Humanoid.Animator:LoadAnimation(script.HoldDrink)
end

There is nothing in output, and the code works dandy, but the animations aren’t playing. I know that they simply aren’t playing because i have Track.Stopped:Wait() in my script and the code after such never runs. Please help, as this is crucial for my experience!

2 Likes

Do you ever play the animations, or do you just load it onto the animator?

1 Like

Yes, here’s the full code if you need it.

local customer = nil
local player = game.Players.PlayerAdded:Wait()

local customers = game.ServerStorage.Customers:GetChildren()

local starts = {"hmm.. dunno what i want yet..", "lovely day for a smoothie, huh?", "surprising this place is open 24 hours.",
	"i'm tired.. sure something on your menu will wake me up.", "this place is open 24 hours? i could never.",
	"hi there!", "everything looks good..", "hmm.. do you have any recommendations?", "i look like i'm thinking but i already know what i want.",
	"what's up?", "this place looks fancy.", "what is up??", "i'm in the mood for a DELICIOUS SMOOTHIE!!", "require smoothie i do."
}

local orders = {"Empty Cup", "Red Cup"}

local endings = {"thank you kindly.", "have a nice day.", "nice doing business with you!", "this is the best day of my WHOLE LIFE.",
	"surprisingly nice service, i'll come back tommorrow.", "maybe i'll come back.", "thanks!", "that was quick.",
	"THIS SERVICE IS BETTER THAN UNICORNS AND RAINBOWS!!!!", "keep the change."
}

local fails = {"should i just come back tommorrow?", "this isn't what i ordered..", "not quite..", "wow. great. i'm so happy.",
	"close enough..", "seriously? i used to come here all the time.", "this is the worst day of my WHOLE LIFE.",
	"never again shall i enter the sorrowing pit that is this smoothie prison.", "do i deserve this?"
}

local CS = game:GetService("Chat")

local function chat(part, msg)
	CS:Chat(part, msg, Enum.ChatColor.Blue)
	part.bass:Play()
end

local function setbeam(boolean)
	for i, v in pairs(game.Workspace.OrderBeam:GetChildren()) do
		if v:IsA("Beam") then
			v.Enabled = boolean
		end
	end
end

local function waituntilplace(order)
	while true do
		game.Workspace.OrderBeam.ProximityPrompt.Triggered:Wait()
		local itemfound = player.Character:FindFirstChild(order)
		if itemfound then
			itemfound.Parent = game.Workspace.InBeam
			itemfound.Handle.CFrame = CFrame.new(-8.325, 4.278, -0.192)
			itemfound.Handle.Anchored = true
			task.wait(0.1)
			game.Workspace.OrderBeam.ProximityPrompt.Enabled = false
			game.Workspace.DeskBell.ClickPart.ClickDetector.MouseClick:Wait()
			return true
		elseif not itemfound and player.Character:FindFirstChildOfClass("Tool") then
			local item2 = player.Character:FindFirstChildOfClass("Tool")
			item2.Parent = game.Workspace.InBeam
			item2.Handle.CFrame = CFrame.new(-8.325, 4.278, -0.192)
			item2.Handle.Anchored = true
			game.Workspace.OrderBeam.ProximityPrompt.Enabled = false
			game.Workspace.DeskBell.ClickPart.ClickDetector.MouseClick:Wait()
			game.SoundService.Fail:Play()
			return false
		end
	end
end

setbeam(false)

local walktrack
local thinktrack
local paytrack
local holdtrack

for i, v in pairs(game.ServerStorage.Customers:GetChildren()) do
	task.wait(1)
	local Humanoid = v:WaitForChild("Humanoid")
	walktrack = Humanoid.Animator:LoadAnimation(script.Walk)
	thinktrack = Humanoid.Animator:LoadAnimation(script.Think)
	paytrack = Humanoid.Animator:LoadAnimation(script.Pay)
	holdtrack = Humanoid.Animator:LoadAnimation(script.HoldDrink)
end

while true do
	task.wait(5)
	local failed = false
	customer = customers[math.random(1, #customers)]
	customer.Parent = game.Workspace
	customer.Humanoid:MoveTo(Vector3.new(-13.607, 3.939, -0.362))
	walktrack:Play()
	game.SoundService.SFX.Bell:Play()
	customer.Humanoid.MoveToFinished:Wait()
	walktrack:Stop()
	customer.LookAtPlayer.Enabled = true
	task.wait(2)
	chat(customer.Head, starts[math.random(1, #starts)])
	thinktrack:Play()
	task.wait(3)
	local order = orders[math.random(1, #orders)]
	thinktrack:Stop()
	setbeam(true)
	
	chat(customer.Head, "i'll take a "..order..", please.")
	game.Workspace.OrderBeam.Ordering.Value = true
	game.Workspace.OrderBeam.ProximityPrompt.Enabled = true
	
	if waituntilplace(order) == true then
		game.Workspace.OrderBeam.Ordering.Value = false
		task.wait(0.1)
		game.Workspace.OrderBeam.ProximityPrompt.Enabled = false
		chat(customer.Head, endings[math.random(1, #endings)])
		local toolinbeam = game.Workspace.InBeam:FindFirstChildOfClass("Tool") 
		toolinbeam.Handle.Anchored = false
		toolinbeam.Parent = customer
		holdtrack:Play()
		game.ReplicatedStorage.Served:FireClient(player)
		setbeam(false)
		paytrack:Play()
		paytrack.Stopped:Wait()
		game.ServerStorage.PaidCash.Parent = game.Workspace
		game.Workspace.PaidCash.Collect:Play()
		task.spawn(function()
			task.wait(2)
			game.Workspace.PaidCash.Parent = game.ServerStorage
		end)
	else
		failed = true
		game.Workspace.OrderBeam.Ordering.Value = false
		task.wait(0.1)
		game.Workspace.OrderBeam.ProximityPrompt.Enabled = false
		chat(customer.Head, fails[math.random(1, #fails)])
		game.Workspace.InBeam:FindFirstChildOfClass("Tool"):Destroy()
		customer.Head.AnswerGui.Wrong.ImageTransparency = 0
		setbeam(false)
	end
	
	customer.LookAtPlayer.Enabled = false
	customer.Humanoid:MoveTo(Vector3.new(-71.412, 3.939, -0.362))
	walktrack:Play()
	customer.Humanoid.MoveToFinished:Wait()
	customer.Parent = game.ServerStorage.Customers
	customer.Head.AnswerGui.Wrong.ImageTransparency = 1
	if failed == false then
		customer:FindFirstChildOfClass("Tool"):Destroy()
	end
	holdtrack:Stop()
	walktrack:Stop()
end

Update: Playtesting it today at this hour, only ONE of the NPCs has their animations working perfectly fine. The other 2 play no animations. What am i doing wrong?

Since you loop through all the humanoids, it only uses the last humanoid, as it replaces everyone else. It might be easier to just insert a script to each customer, so it deals with their own individual humanoid.

How would i play the animation track from another humanoid with a script that hasn’t loaded it?

No, I mean to load and play the animations inside a script parented to the individual customers, so they deal with their humanoids without any issues.

Ok. Can you explain more precisely what you think the problem is, so i might be able to think of an easier fix?

Alright. I think the problem is when you loop through the customer’s humanoid to load the animations, you never save each individual humanoid, rather replacing it in the next iteration of the loop. That is why only ONE of the NPCs play the animation, because you only received ONE humanoid, which is the one you use to play the animations.

1 Like

I marked this as a half-solution as i hadn’t tried it yet but it sounded like it would work. Sorry to nag you, but how would i go about this? every time the for loops, i save that customers humanoid into a variable, and play the animations with that variable? Again, apologies for the many inconveniences, but i’ve never had this issue before.

It’s okay. You can try to insert each humanoid into a table, and use them however you wish. It should look something like this:

local Humanoids = {}

for i, v in pairs(game.ServerStorage.Customers:GetChildren()) do
	task.wait(1)
	local Humanoid = v:WaitForChild("Humanoid")
	
	table.insert(Humanoids, Humanoid.Parent)
	
	walktrack = Humanoid.Animator:LoadAnimation(script.Walk)
	thinktrack = Humanoid.Animator:LoadAnimation(script.Think)
	paytrack = Humanoid.Animator:LoadAnimation(script.Pay)
	holdtrack = Humanoid.Animator:LoadAnimation(script.HoldDrink)
end

(I haven’t tested it, so inform me if anything doesn’t work!)

1 Like

And how would i play the animation for the customer’s specific humanoid? something involving table.find?

Yeah, you can use table.find(). For convenient sake, name the NPC’s something different every time you spawn one, so you can have a more effective table.find().

I’ve set up

customerhum = table.find(Humanoids, customer.Name)

but how can i make sure it plays the animation for the customerhum variable?

Try this:

customerhum = table.find(Humanoids, customer.Name)

local AnimTrack = customerhum:WaitForChild("Humanoid").Animator:LoadAnimation() -- put your animation here
AnimTrack:Play()

You don’t need to load the animation beforehand, as it isn’t really necessary. Loading on the spot works the same.

1 Like

I just now realized that earlier i was getting an error that said i reached the limit for animationtracks, but that was being caused by a different script. I deleted that script, but before that i edited the CustomerHandler as i thought it was the problem. I basically reached a rabbit hole of editing and forgot my main goal. I appreciate your help too much!

1 Like

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