I’m encountering an issue for my civilian AI where if the player gets close to the character, they start stuttering in their movements.
Any help will be greatly appreciated!!
Script:
local pathfindingService = game:GetService("PathfindingService")
local pathsetDecided = false
local movementDebounce = false
local chosenPathset = nil
local pathNumber = 0
while true do
if pathsetDecided == true and chosenPathset ~= nil then
if movementDebounce == false then
movementDebounce = true
pathNumber = pathNumber + 1
if #chosenPathset:GetChildren() < pathNumber then
local civSystem = game.ReplicatedStorage.SpawnedCivs:FindFirstChild(script.Parent.Parent.Name)
if civSystem then
civSystem.Parent = game.ReplicatedStorage.Civilians
end
script.Parent.Parent:Destroy()
break
end
local chosenPath = chosenPathset:FindFirstChild("waypoint"..pathNumber)
if ("waypoint"..#chosenPathset:GetChildren()) >= chosenPath.Name then
local path = pathfindingService:CreatePath()
path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position, chosenPath.Position)
for i, waypoint in pairs(path:GetWaypoints()) do
script.Parent.Parent.Humanoid:MoveTo(waypoint.Position)
script.Parent.Parent.Humanoid.MoveToFinished:Wait()
movementDebounce = false
end
end
end
else
local pathChoices = game.Workspace.CivWaypoints:GetChildren()
local chosenPathNumber = math.random(1, #pathChoices)
chosenPathset = pathChoices[chosenPathNumber]
script.Parent.Parent.HumanoidRootPart.CFrame = chosenPathset.waypoint1.CFrame
pathsetDecided = true
end
wait(0.1)
end
local pathfindingService = game:GetService("PathfindingService")
local pathsetDecided = false
local movementDebounce = false
local chosenPathset = nil
local pathNumber = 0
while true do
if pathsetDecided == true and chosenPathset ~= nil then
if movementDebounce == false then
movementDebounce = true
pathNumber = pathNumber + 1
if #chosenPathset:GetChildren() < pathNumber then
local civSystem = game.ReplicatedStorage.SpawnedCivs:FindFirstChild(script.Parent.Parent.Name)
if civSystem then
civSystem.Parent = game.ReplicatedStorage.Civilians
end
script.Parent.Parent:Destroy()
break
end
local chosenPath = chosenPathset:FindFirstChild("waypoint"..pathNumber)
if ("waypoint"..#chosenPathset:GetChildren()) >= chosenPath.Name then
local path = pathfindingService:CreatePath()
path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position, chosenPath.Position)
for i, waypoint in pairs(path:GetWaypoints()) do
script.Parent.Parent.Humanoid:MoveTo(waypoint.Position)
script.Parent.Parent.Humanoid.MoveToFinished:Wait()
movementDebounce = false
end
end
end
else
local pathChoices = game.Workspace.CivWaypoints:GetChildren()
local chosenPathNumber = math.random(1, #pathChoices)
chosenPathset = pathChoices[chosenPathNumber]
script.Parent.Parent.HumanoidRootPart.CFrame = chosenPathset.waypoint1.CFrame
pathsetDecided = true
end
wait(0.1)
end
(I tried to put a video up yet it kept failing to upload sadly)