NPC animation doesnt play at a distance

I have a velociraptor with a follow script (server) and an animation script(server). Everything works except for the animation. It only plays when you are about 10ish studs close. When you are further it just stops.

script.Parent.PrimaryPart:SetNetworkOwner(nil)

function FindTarget()
	local maxDistance = 2000
	local target = nil
	for i,v in pairs(workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") then
			if (v.Head.Position-script.Parent.Head.Position).magnitude < maxDistance then
				target = v.Head
			end
		end
	end
	return target
end

while wait(0.1) do
	local target = FindTarget()
	if target then
		script.Parent.Humanoid:MoveTo(target.Position)
	end
end

Animate script >

local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local Animator = Humanoid:FindFirstChild("Animator")

--Animations--
local Walk = script:FindFirstChild("WalkAnimation")


--Tracks--
local WalkTrack = Animator:LoadAnimation(Walk)




Humanoid.Running:Connect(function(Speed)
	
	if Speed > 0 then
		if not WalkTrack.IsPlaying then
			WalkTrack:Play()	
		end
		
	else
		if WalkTrack.IsPlaying then
			WalkTrack.DidLoop:Connect(function()
				WalkTrack:Stop()
			end)
		end
	end
end)

I’m not even sure if this contributes to the problem but, from what I also know, checking if speed > 0 has some jankiness to it as it’s not like it’s firing every time the velocity of the root part of the humanoid changes. So, instead, check to see if speed > 0.01.

But, addressing to your problem, try loading it with ContentProvider. You could preload the animation so that it doesn’t have to load when it actually needs to load, which could be the reason why it’s playing in a delay.

It could also be that you have StreamingEnabled too, probably. I think it actually prevents things from loading/playing if it’s outside the max distance that you’ve set. This goes for physics and any other assets, they do this so that it boosts performance by loading things in chunks instead of loading all the assets at once.

2 Likes

Update: I analysed the roblox character animate script and they used speed > 0.01. I dont think humanoid.running event is the issue. Streaming enabled is also turned off. I also checked to see if the animation works in server mode. It does work in server view so i think its nothing about roblox client and throttling. This bug also happens in a normal roblox game server not just in studio.

Again, you tried preloading it in the script? I’ll see if there’s more information about this around in the forum

1 Like

I tried preloading it already. Still the same result. The dinosaur animation only plays when the range is close.

UPDATE: In studio, i tried dragging and rotating the dinosaur outside the range of the animation playing. The animation plays once and then stops.

UPDATE: I used print every time the running event fired. It only fires when the dinosaur is close and it doesnt when the dinosaur is far. I think i should loop the animation and only stop it when the dinosaur isnt moving to fix the issue.

I used moon animator to animate it and forgot to check the loop setting.z

Solution: Before uploading the animation enable the loop setting.

1 Like

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