Ai repeatedly moving to multiple spots at the same time

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)


Right here. Maybe try putting Wait(1) or Wait(2) see if that works

1 Like

Nope, it doesn’t do anything. Any other clues?

1 Like

It also seems like your trying to run the function 2 times?

Is there any explanation for this? Maybe removing the top function.

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)
1 Like

Also if the AI seems kinda janky when you get this working you can try changing the Network owner of the NPC at start of the script.

NPC.PrimaryPart:SetNetworkOwner(nil)
1 Like

Wow! Thank you so much!! 303030303030