basically, I have an npc of which if the NPC is more than 7 (in magnitude) away from you, the NPC will walk towards you, if magnitude is less then 7, then the NPC will stop as it is “Too Close”, the problem is though, the other if statement is if the magnitude is greater than 20, the NPC should change its walkspeed to 20, and a sprinting animation should play (as the one seen in the video), however it doesnt work, no errors either.
VIDEO:
CODE:
local Char = script.Parent
local Humanoid = Char:FindFirstChild("Humanoid")
local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
local Head = Char:WaitForChild("Head")
local Walk = Humanoid.Animator:LoadAnimation(Char.Walk)
local Run = Humanoid.Animator:LoadAnimation(Char.Animation)
local Idle = Humanoid.Animator:LoadAnimation(Char.Idle)
local RunService = game:GetService("RunService")
--local Jump = Humanoid.Animator:LoadAnimation(Char.Jump)
Char.PrimaryPart:SetNetworkOwner(nil)
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
Humanoid.Running:Connect(function(speed)
--print("NPC Speed is :" ..speed)
if speed > 0 then
--Walk
Idle:Stop()
Walk:Play()
elseif speed > 17 then
--Run
print("Should Be Running")
Idle:Stop()
Walk:Stop()
Run:Play()
else
--Stop
Walk:Stop()
Run:Stop()
Idle:Play()
end
end)
local PathfindingService = game:GetService("PathfindingService")
local function getPath(destination)
local path = PathfindingService:CreatePath()
path:ComputeAsync(HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 7 then
print("NPC is Walking")
Humanoid.WalkSpeed = 6
Humanoid:MoveTo(target.HumanoidRootPart.Position)
elseif distance > 20 then
print("Distance is greater than 20; Should be Running")
Humanoid.WalkSpeed = 20
elseif distance < 7 then
print("Too Close, NPC Stopped")
Humanoid.WalkSpeed = 0
end
--[[
if distance > 4 then --not close enough
Humanoid:MoveTo(target.HumanoidRootPart.Position)
Humanoid.WalkSpeed = 6
else --close
Humanoid.WalkSpeed = 0
end
-]]
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target then
--print("TARGET FOUND",target.Name)
attack(target)
break
else
--print("Moving to", waypoint.Position)
Humanoid:MoveTo(waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
end
else
Humanoid:MoveTo(destination.Position - (HumanoidRootPart.CFrame.LookVector * 10)) --NPC walk back if hit wall
end
end
local function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while task.wait(.5) do
patrol()
end