(Idk why the Video doesnt work the first 10 secs)
–Problem
if WaypointPlayerDistance <= 4 and PlayerDistance <= 4 then
NpcHrp.CFrame = CFrame.lookAt(NpcHrp.Position, NpcHrp.Position + ((NearestPlayer.Character.HumanoidRootPart.Position - NpcHrp.Position).Unit * Vector3.new(1,0,1)))
Punch1:Play()
HitBoxModule.new(script.Parent, Vector3.new(5,5.5,6), Vector3.new(4,0,0), 5, 0.07, 0.43, "Punch1")
task.wait(0.5)
break
end
–Entire Script
local RunService = game:GetService("RunService")
local PFS = game:GetService("PathfindingService")
local Modules = game:GetService("ServerScriptService").Modules
local AbbreviateModule = require(Modules.AbbreviateNumber)
local HitBoxModule = require(Modules.HitBox)
local Humanoid = script.Parent.Humanoid
local NpcHrp = script.Parent.HumanoidRootPart
local SpawnPoint = script.Parent.Parent
local animator = Humanoid:WaitForChild("Animator")
local Punch1 = animator:LoadAnimation(game.ServerScriptService.Animations:WaitForChild("SimplePunch1"))
local path = PFS:CreatePath()
local Distance
local NearestPlayer
local Waypoints
local PlayerDistance
local Debounce = false
Humanoid.AutoRotate = true
RunService.Heartbeat:Connect(function()
if Humanoid.Health == 0 then
script.Parent:Destroy()
end
end)
while Debounce == false do -- NPC Cycle
Debounce = true
local ShortestDistance = math.huge
local WaypointPlayerDistance = math.huge
repeat -- checks every Player and their distance until one is <= 30 away
for i, player in game.Players:GetPlayers() do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then -- check if Player is ingame
Distance = (SpawnPoint.Position - player.Character.HumanoidRootPart.Position).Magnitude
if Distance < ShortestDistance then
ShortestDistance = Distance
NearestPlayer = player
end
end
end
task.wait()
until ShortestDistance <= 30
repeat
spawn(function() -- follow player
RunService.Heartbeat:Connect(function()
for i, player in game.Players:GetPlayers() do
if NearestPlayer.Character and NearestPlayer.Character:FindFirstChild("HumanoidRootPart") then -- check if Player is ingame
Distance = (SpawnPoint.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude
PlayerDistance = (NpcHrp.Position - NearestPlayer.Character.HumanoidRootPart.Position).Magnitude
--print(Distance)
if Distance < ShortestDistance then
ShortestDistance = Distance
end
end
end
end)
end)
if Distance <= 30 and NearestPlayer.Character.Humanoid.Health ~= 0 then
while PlayerDistance == nil do wait() end
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, NearestPlayer.Character.HumanoidRootPart.Position)
Waypoints = path:GetWaypoints()
for i, waypoint in pairs(Waypoints) do
WaypointPlayerDistance = ((waypoint.Position - NearestPlayer.Character.HumanoidRootPart.Position) * Vector3.new(1,0,1) + Vector3.new(0,3,0)).Magnitude
if Distance >= 30 and NearestPlayer.Character.Humanoid.Health == 0 then break end
if WaypointPlayerDistance <= 4 and PlayerDistance <= 4 then
NpcHrp.CFrame = CFrame.lookAt(NpcHrp.Position, NpcHrp.Position + ((NearestPlayer.Character.HumanoidRootPart.Position - NpcHrp.Position).Unit * Vector3.new(1,0,1)))
Punch1:Play()
HitBoxModule.new(script.Parent, Vector3.new(5,5.5,6), Vector3.new(4,0,0), 5, 0.07, 0.43, "Punch1")
task.wait(0.5)
break
end
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Humanoid:MoveTo(waypoint.Position)
end
end
until Distance >= 30 or NearestPlayer.Character.Humanoid.Health == 0
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, SpawnPoint.Position) -- Go back to Spawn
local Waypoints = path:GetWaypoints()
for i, waypoint in pairs(Waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Humanoid:MoveTo(waypoint.Position)
end
Debounce = false
end