so this is the code
script.Parent:SetPrimaryPartCFrame(workspace.GuardsSpawnHere.CFrame)
--wait(math.random(2,10))
local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath()
local path2 = pathfinding:CreatePath()
local walk = script.Parent.Humanoid:LoadAnimation(game.ServerStorage.Walk)
local jump = script.Parent.Humanoid:LoadAnimation(game.ServerStorage.Jump)
walk:Play()
local guarding = true
local currentpath = path
local function peoplenearme(guard)
local shortest,person = 30,nil
for _,i in pairs(game.Players:GetChildren()) do
local dis = (i.Character.HumanoidRootPart.Position - guard.HumanoidRootPart.Position).Magnitude
if dis <= shortest then
shortest,person = dis,i.Character
end
end
return person
end
local destenation = workspace.GoHere
while wait(1) do
if (script.Parent.HumanoidRootPart.Position - destenation.Position).Magnitude > 8 then
path:ComputeAsync(script.Parent.HumanoidRootPart.Position,workspace.GoHere.Position)
local waypoints = path:GetWaypoints()
for i,waypoint in pairs(waypoints) do
local attack = peoplenearme(script.Parent)
if attack ~= nil then
destenation = attack.HumanoidRootPart
guarding = false
path:ComputeAsync(script.Parent.HumanoidRootPart.Position,attack.HumanoidRootPart.Position)
waypoints = path:GetWaypoints()
else
if guarding == false then
guarding = true
path:ComputeAsync(script.Parent.HumanoidRootPart.Position,workspace.GoHere.Position)
waypoints = path:GetWaypoints()
end
end
if path.Status == Enum.PathStatus.NoPath then
waypoints = path:GetWayPoints()
walk:Stop()
if not guarding then
guarding = true
end
else
if not walk.IsPlaying then
walk:Play()
end
end
if waypoint.Action == Enum.PathWaypointAction.Jump then
walk:Stop()
jump:Play()
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
script.Parent.Humanoid:MoveTo(waypoint.Position)
script.Parent.Humanoid.MoveToFinished:Wait(2)
end
else
walk:Stop()
end
end
i know it is a lot of code so i will walk you threw it so first in the while it checks that it has not reached the target point then it creates a path then it gets var attack and the function players near me get the closest player with a maximum distance of 30 studs and i want it so if there is a player then it will switch targets i attempted to do that by doing
path:ComputeAsync(script.Parent.HumanoidRootPart.Position,attack.HumanoidRootPart.Position)
waypoints = path:GetWaypoints()
but for some reason it didn't work can anybody help me make the path switch targets?
EDIT: i solved it so you need to make the for loop only go if a var named chaged (or somthing) is false then after the for make it false so if you want to change it make it be true so the loop won't run then will start over going to the new destination
