Hi, I’m having some difficulty with the enemy NPC in my game. Before it would chase the player just fine and follow the player around and damage it. Now it glitches and runs away from the player or glitches. Here’s how it looks like.
https://i.gyazo.com/35b3828957b0ad051eda17dc9d079564.mp4
https://i.gyazo.com/eff9bebc8c76b318351379bea71b129e.mp4
This is the NPC follow script,
local function findPlayer()
for i,v in pairs(game.Players:GetPlayers()) do
if not v.Character then return end
if v.Character:FindFirstChildOfClass("Humanoid") then
if v.Character:FindFirstChild("HumanoidRootPart") then
local Hum = script.Parent:FindFirstChild("Humanoid")
local vsRoot = v.Character:FindFirstChild("HumanoidRootPart")
if Hum and vsRoot then
if script.Parent:FindFirstChild("HumanoidRootPart") then
local Root = script.Parent:FindFirstChild("HumanoidRootPart")
if (Root.Position - vsRoot.Position).Magnitude <= 50 then
Hum:MoveTo(vsRoot.Position)
if script.Parent:FindFirstChild("Follow") then
script.Parent:FindFirstChild("Follow").Value = true
end
else
Hum:MoveTo(Oldpos)
if script.Parent:FindFirstChild("Follow") then
script.Parent:FindFirstChild("Follow").Value = false
end
return Hum,true
end
end
end
end
end
end
end
spawn(function()
while true do
wait()
if script.Parent:FindFirstChild("Humanoid").Health > 0 then
findPlayer()
end
end
end)
Any way to fix this? Any help is appreciated. Thanks.