I simply want it to go to the nearest torso, at this point he runs back and fourth from the first torso he found to the nearest, causing allot of issues.
Ive tried moving around multiple things, looping etc.
here is my code
hum = script.Parent:WaitForChild("Humanoid")
local anim = script:WaitForChild("Idle")
local sliter = script:WaitForChild("Slithering")
local distance = 50
local torsos = {}
local idle = hum:LoadAnimation(anim)
local slitt = hum:LoadAnimation(sliter)
local atk = script:WaitForChild("Bite/SpitVenom")
atkload = hum:LoadAnimation(atk)
local pfs = game:GetService("PathfindingService")
local tc = math.huge
local hr = script.Parent.HumanoidRootPart
local atking = false
local dmg = false
local torsobeingfollowed = nil
patinfo = {["AgentHeight"] = 0,
["AgentRadius"] = 2,
}
path = pfs:CreatePath(patinfo)
hum.Running:connect(function(spd)
wait()
if spd<= 0 then
wait()
hum.WalkSpeed = 0
idle:Play()
wait()
end
end)
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:GetPropertyChangedSignal("WalkSpeed"):connect(function()
wait()
if hum.WalkSpeed == 0 then
wait()
slitt:Stop()
idle:Play()
wait()
elseif hum.WalkSpeed == 8 then
wait()
idle:Stop()
slitt:Play()
wait()
end
end)
hum.WalkSpeed = 0
function follow()
for i,v in pairs(torsos) do
local b1 = script.Parent.Head
local bmag = (b1.Position - v.UpperTorso.Position).magnitude
if bmag < distance and bmag < tc and v.Humanoid.Health > 0 then
tc = bmag
torsobeingfollowed = v.UpperTorso
path:ComputeAsync(b1.Position,torsobeingfollowed.Position)
path.Blocked:connect(function()
print("blocked")
path:ComputeAsync(b1.Position,torsobeingfollowed.Position)
print("newpath")
end)
local wp = path:GetWaypoints()
for _,wp in pairs(wp) do
hum:MoveTo(wp.Position)
if bmag < 10 and bmag > 0 then
print("new")
hum.WalkSpeed = 0
idle:Stop()
if not idle.IsPlaying and not atkload.IsPlaying and dmg == false then
print("Can Attack")
atkload:Play()
wait(0.5)
v.Humanoid:TakeDamage(20)
dmg = false
end
elseif bmag > 10 and bmag < distance then
print("within range")
atkload:Stop()
hum.WalkSpeed = 8
end
wait(.1)
tc = math.huge
end
end
end
end
coroutine.resume(coroutine.create(function()
while wait() do
follow()
end
end))