Hello i am making a npc that if you get near to it will start following you and start attacking when he gets too close but the npc doesn't move so can any one tell me what is wrong with this script? btw it doesn't say anything in the output
game.Loaded:Wait()
local Torso = script.Parent.Torso
local Target = nil
local LockWhileTrueLoop = false
local Animator = script.Parent.Humanoid.Animator
local Animations = {
["Attack"] = Animator:LoadAnimation(script.Attack),
["Walk"] = Animator:LoadAnimation(script.Walk)
}
function Patrol()
if #game.Players:GetChildren() > 0 then
for i,v in pairs(game.Players:GetChildren()) do
if ((v.Character.Torso.Position - Torso.Position).Magnitude < 50) then
LockWhileTrueLoop = true
Target = v.Character
LockWhileTrueLoop = false
end
wait(0.1)
end
end
end
function FollowPlayer()
repeat
if not ((Target.Torso.Position - Torso.Position).Magnitude < 4) then
LockWhileTrueLoop = true
Animations["Walk"]:Play()
script.Parent.Humanoid:MoveTo(Target.Torso.Position)
script.Parent.Humanoid.MoveToFinished:Wait()
Animations["Walk"]:Stop()
LockWhileTrueLoop = false
else
LockWhileTrueLoop = true
Animations["Attack"]:Play()
wait(0.06)
if ((Target.Torso.Position - Torso.Position).Magnitude < 4) then
Target.Humanoid:TakeDamage(3)
end
LockWhileTrueLoop = false
end
until Target.Humanoid.Health == 0 or ((Target.Torso.Position - Torso.Position).Magnitude > 100)
end
while true do
if Target == nil then
Patrol()
else
FollowPlayer()
end
wait(0.1)
end
this is all inside a normal script in the npc

2 comments
There were still some bugs but i fixed them
Nice