Hello, I’m making a zombie game right now, the zombies go to you, damage you, and are decently smart, but the problem is that they can never catch up to you, even if they are faster. This is because my position is constantly changing, so every time they go to where they thought I was positioned, It turns out I’m like 6 studs away. I’m wondering if there is a way to make them catch up to you?
Script
local PathfindingService = game:GetService("PathfindingService")
local DebrisService = game:GetService("Debris")
local manager = require(script.Parent.ZombieManager)
game.Players.PlayerAdded:Wait()
function FindClosestPlayer(pos)
local character = nil
local closestDistance = math.huge
local players = game.Players:GetChildren()
for i=1, #players do
local Character = players[i].Character or players[i].CharacterAdded:Wait()
if (Character.PrimaryPart.Position - pos).Magnitude < closestDistance then
closestDistance = (Character.PrimaryPart.Position - pos).Magnitude
character = Character
end
end
return character
end
function FindClosestDistance(pos)
local character = nil
local closestDistance = math.huge
local players = game.Players:GetChildren()
for i=1, #players do
local Character = players[i].Character or players[i].CharacterAdded:Wait()
if (Character.PrimaryPart.Position - pos).Magnitude < closestDistance then
closestDistance = (Character.PrimaryPart.Position - pos).Magnitude
end
end
return closestDistance
end
while wait(0.2) do
local zombies = workspace.ZombieFolder:GetChildren()
if #zombies > 0 then
for i,v in ipairs(zombies) do
zombies[i].Humanoid:LoadAnimation(zombies[i].Humanoid.Walk):Play()
local follower = zombies[i]
local Humanoid = follower.Humanoid
local Distance = FindClosestDistance(follower.PrimaryPart.Position)
local EndDestination = FindClosestPlayer(follower.PrimaryPart.Position)
local path = PathfindingService:CreatePath()
path:ComputeAsync(follower.HumanoidRootPart.Position, EndDestination.PrimaryPart.Position)
if Humanoid.Health == 0 then
DebrisService:AddItem(follower, 5)
follower.Parent = workspace.DeadZombies
else
if path.Status == Enum.PathStatus.Success then
local nodes = path:GetWaypoints()
task.spawn(function()
for i= 1,(#nodes - 1) do
Humanoid:MoveTo(nodes[i+1].Position)
if Distance < 5 then
EndDestination.Humanoid.Health -= follower:GetAttribute("Damage")
end
end
end)
end
end
end
end
end
If you need other things or have tips, please tell me. Thank you!
Link, Alpha Force - Roblox