I want it to follow normally, and the idle animation only to play when it is not moving
The problem is, every since I added path finding, it seems like its not moving at its regular speed, the pathfinding is quite stupid sometimes, and the idle animation sometimes plays when its following you, this is my first time with pathfinding. Also no errors in output at all.
I have tried switching multiple lines of the code around, I think the idle problem is due to the pathfinding in general, and the slowness may be a problem with pathfinding looping so much, but I cant figure a way out around it.
hum = script.Parent:WaitForChild("Humanoid")
local anim = script:WaitForChild("Idle")
local sliter = script:WaitForChild("Slithering")
local distance = 10
local torsos = {}
local idle = hum:LoadAnimation(anim)
local slitt = hum:LoadAnimation(sliter)
local pfs = game:GetService("PathfindingService")
patinfo = {["AgentHeight"] = 10,
["AgentRadius"] = 1,
["AgentCanJump"] = false
}
path = pfs:CreatePath(patinfo)
game.Players.PlayerAdded:connect(function(plyr)
plyr.CharacterAdded:connect(function(char)
repeat wait() until char.UpperTorso
table.insert(torsos,char)
print(torsos[1])
game.Players.PlayerRemoving:connect(function(plyr)
table.remove(torsos,der)
end)
end)
end)
hum.Running:connect(function(spd)
if spd <= 0 then
idle:Play()
elseif spd > 0 then
idle:Stop()
slitt:Play()
else
end
end)
function follow()
for i,v in pairs(torsos) do
local b1 = script.Parent.Body1
local bmag = (b1.Position - v.UpperTorso.Position).magnitude
if bmag < distance then
path:ComputeAsync(b1.Position,v.UpperTorso.Position)
local wp = path:GetWaypoints()
for _,wp in pairs(wp) do
hum:MoveTo(wp.Position)
hum.MoveToFinished:Wait()
end
end
end
end
coroutine.resume(coroutine.create(function()
while wait() do
follow()
end
end))