-
What do you want to achieve? My nextbot to chase the player without any delays and sudden stops.
-
What is the issue? So I have a little nextbot game and the nextbot is delaying a bit and sometimes just suddenly stops. I feel like it’s delayed because the position of the player doesn’t get updated too frequently and the nextbot tries to reach the previous location of the player.
-
What solutions have you tried so far? I’ve tried wrapping the path computing line in a
while task.wait() do
loop but that just stopped the rest of the code from running, even when I wrapped the while loop in a task.spawn() function.
I apologize for long code. I have tried to organize it just for the sake of readability and understandability.
Here’s also the game link so you guys could try it out real quick: old school we had but got demolished - Roblox
local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")
local NextbotHRP = script.Parent:WaitForChild("HumanoidRootPart")
local NextbotHumanoid = script.Parent:WaitForChild("Humanoid")
NextbotHRP.ChildAdded:Connect(function(Child)
if Child:IsA("Sound") and Child.Name ~= "Idle Sound" then
Child:Destroy()
end
end)
-- Set the health of the nextbot's humanoid to infinite.
NextbotHumanoid.MaxHealth = math.huge
NextbotHumanoid.Health = NextbotHumanoid.MaxHealth
local ClosestHrp = nil
local Distance = nil
local MaxDistance = 500
local function CalculatePath()
local Path = PathfindingService:CreatePath()
Path:ComputeAsync(NextbotHRP.Position, ClosestHrp.Position)
local Waypoints = Path:GetWaypoints()
for _, waypoint in pairs(Waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
NextbotHumanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
NextbotHumanoid:MoveTo(waypoint.Position)
NextbotHumanoid.MoveToFinished:Wait()
end
end
while task.wait() do
for _, player in ipairs(Players:GetPlayers()) do
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid.Health > 0 then
local DistanceBetween = (HumanoidRootPart.Position - NextbotHRP.Position).Magnitude
if Distance ~= nil then
if DistanceBetween <= Distance and DistanceBetween < MaxDistance or #Players:GetPlayers() == 1 then
Distance = DistanceBetween
ClosestHrp = HumanoidRootPart
CalculatePath()
end
else
if DistanceBetween < MaxDistance then
Distance = DistanceBetween
ClosestHrp = HumanoidRootPart
CalculatePath()
end
end
end
end
NextbotHumanoid.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
if Players:GetPlayerFromCharacter(hit.Parent) then
hit.Parent:FindFirstChild("Humanoid").Health = 0
end
end
end)
end