Hey, my AI isn’t follow me because it’s sometimes spammed.
Any idea how to fix?
wait(0.5)
local Bear = game:GetService("ServerStorage").BlackBear
local BearHumanoid = Bear.Humanoid
local PathFindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Settings = script:WaitForChild("Settings")
local Player = script.Parent
local PlayerHumanoidRoot = Player.HumanoidRootPart
Bear.Parent = workspace
local MaxDistance = Settings.aiMaxDistance.Value
local loadrun = BearHumanoid:LoadAnimation(script.Running)
local RunService = game:GetService("RunService")
loadrun:Play()
local function FollowPlayer()
local Path = PathFindingService:CreatePath()
local MovingToPosition = PlayerHumanoidRoot.Position - Vector3.new(5,0,0)
local ComputedPath = Path:ComputeAsync(Bear.HumanoidRootPart.Position, MovingToPosition)
return Path
end
RunService.Stepped:Connect(function()
FollowPlayer()
local Path = FollowPlayer()
for Index, Waypoint in pairs(Path:GetWaypoints()) do
BearHumanoid:MoveTo(Waypoint.Position)
BearHumanoid.MoveToFinished:Wait()
local Part = Instance.new("Part",workspace)
Part.Anchored = true
Part.Position = Waypoint.Position
end
end)
Since it’s a connection it won’t yield at the MoveToFinished:Wait() (or anywhere since it runs every time the Stepped is fired).
You can try making a debounce so it checks if the loop is already running.
Alternatively you can just use a while wait() loop.
Example:
local Debounce = false
RunService.Stepped:Connect(function()
if Debounce == false then
Debounce = true
FollowPlayer()
local Path = FollowPlayer()
for Index, Waypoint in pairs(Path:GetWaypoints()) do
BearHumanoid:MoveTo(Waypoint.Position)
BearHumanoid.MoveToFinished:Wait()
local Part = Instance.new("Part",workspace)
Part.Anchored = true
Part.Position = Waypoint.Position
end
Debounce = false
end
end)