I am trying to make a NPC to follow the player. It is following the player, but slightly. Because I am trying to fix this problem, where the NPC would move to the last playerRootPart position instead of the current one. So basiclly if the player moves, the npc would move where the last position was, it isn’t following along with the player.
What I have tried:
I tried creating a recompute function, where if the player position is greater than its current position then create a new path. But it isn’t doing that.
Here is the script:
local PathfindingService = game:GetService("PathfindingService")
local AttackNPC = script.Parent
local zombie = AttackNPC:FindFirstChild("Humanoid")
local NPCRootPart = AttackNPC:FindFirstChild("HumanoidRootPart")
local isNPCBlocking = AttackNPC:FindFirstChild("isNPCBlocking")
local punch1 = AttackNPC:FindFirstChild("Combo1")
local punch2 = AttackNPC:FindFirstChild("Combo2")
local gettingHit = AttackNPC:FindFirstChild("GettingHit")
local attackCooldown = 0.7
local damage = 2.5
local hitSpeed = 2
local RecomputePathFrequency = 1
LastRecomputePath = 0
local Recomputing = false
charactersFound = false
nearestCharacterFound = false
target = nil
local canAttack = true
whoHitNPC = {}
local function checkDistance(part1,part2)
magnitude = (part1.Position - part2.Position).Magnitude
return magnitude
end
function scanningForTarget()
for i,v in pairs(game.Workspace:GetChildren("Model")) do
if v:IsA("Model") then
if v ~= AttackNPC then
if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
local playerCharacter = v
characterRootPart = v:FindFirstChild("HumanoidRootPart")
charactersFound = true
nearestCharacterFound = false
moveToTarget()
end
end
end
end
end
function moveToTarget()
if checkDistance(NPCRootPart,characterRootPart) < 100 then
target = characterRootPart.Position
nearestCharacterFound = true
path = PathfindingService:CreatePath()
path:ComputeAsync(NPCRootPart.Position,characterRootPart.Position)
local waypoints = path:GetWaypoints()
for _,waypoint in pairs(waypoints) do
RecomputePath()
zombie:MoveTo(target)
zombie.MoveToFinished:Wait()
if characterRootPart.Parent.Humanoid.Health <= 0 then
target = nil
scanningForTarget()
end
end
end
end
function RecomputePath()
wait(RecomputePathFrequency)
if target >= target and Recomputing == false then
Recomputing = true
path:ComputeAsync(NPCRootPart.Position,characterRootPart.Position)
wait(RecomputePathFrequency)
Recomputing = false
end
end
function Attack()
local currentWalkSpeed = target.Parent.Humanoid.WalkSpeed
if checkDistance(NPCRootPart,characterRootPart) < 3.5 then
if target ~= nil and zombie.Health > 0 and canAttack == true then
local combo1Track = zombie:LoadAnimation(punch1)
combo1Track:Play()
target.Parent.Humanoid:TakeDamage(damage)
hitTrack = target.Parent.Humanoid:LoadAnimation(gettingHit)
hitTrack:Play()
target.Parent.Humanoid.WalkSpeed = hitSpeed
hitTrack:Stop()
wait(1)
target.Parent.Humanoid.WalkSpeed = currentWalkSpeed
wait(attackCooldown)
if checkDistance(NPCRootPart,characterRootPart) < 3.5 and canAttack == true then
local combo2Track = zombie:LoadAnimation(punch2)
combo2Track:Play()
target.Parent.Humanoid:TakeDamage(damage)
hitTrack:Play()
target.Parent.Humanoid.WalkSpeed = hitSpeed
hitTrack:Stop()
wait(1)
target.Parent.Humanoid.WalkSpeed = currentWalkSpeed
end
end
end
end
function main()
if target ~= nil and nearestCharacterFound == true and target.Parent.Humanoid.Health > 0 then
moveToTarget()
Attack()
return true
elseif target == nil then
scanningForTarget()
end
end
while wait() do
main()
end