Animation Doesn't Work In-Game?

My NPC walking animation runs in-studio Run mode perfectly, but when you go in the actual game it stops working.

ezgif.com-video-to-gif

local i = 0
local touched = false
local anim = script.Parent.Walking
local animTrack = script.Parent.Humanoid:LoadAnimation(anim)
animTrack.Looped = true

wait(5)

animTrack:Play()

repeat
	i = i + 1

	script.Parent.PrimaryPart = script.Parent.Torso
	script.Parent:SetPrimaryPartCFrame(script.Parent.Torso.CFrame + Vector3.new(0,0,0.16))
	wait()
	
script.Parent.Torso.Touched:Connect(function(hit)
	if hit.Name == "Torso" and not touched then
		animTrack:Stop()
		print(hit.Name .. " has been touched by " .. script.Parent.Name)
		touched = true
		i = 9999999
		for h = 0, 100, 1 do
			local dmg = math.random(1,50)
			wait(2.5)
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - dmg
		end
	end
end)
until i == 9999999
1 Like

First of all, constantly doing a connection on a part will really hurt performance heavily (of course if the part isn’t destroy:() ed) so I’d recommend disconnecting it or just having the connection be done once.

Another thing, this may be your issue though I’m not sure:

script.Parent.Torso.Touched:Connect(function(hit)
	if hit.Name == "Torso" and not touched then
		animTrack:Stop() -- This line gets called alot of times, stops the animation.

The answer you provided did not work…

If you are playing the animation on the server, it will actually (usually) play before the player finishes loading into the game, meaning it won’t sync since the player hasnt loaded the animation yet before its been played.
For testing purposes make a countdown from like 15 or so and then try.
Also please don’t ever do that because you are creating millions of connections because Connect makes a new one every time it is called

How do I do something other than “connect”

You don’t, that’s not the point
Either use :Wait() or use :Connect() properly as to not flood an object with thousands and thousands of connections