My rig keeps on jittering and i need help fixing this problem!
When the path gets blocked, the number of waypoints suddenly just double!
I’ve tried reviewing my code and using the dev forum to my advantage but to no avail.
heres my mdule script that i used!
its rlly messy im sorry please give me feedback but most importantly help me through this problem of mine.
local policemodule = {}
local enemy = "Criminal"
function findnearest(root)
local maxdist = 300
local nearest = nil
for i, v in pairs(workspace:GetChildren()) do
local vhum = v:FindFirstChild("Humanoid")
local vroot = v:FindFirstChild("HumanoidRootPart")
local status = v:FindFirstChild("Type")
if vhum and vroot and status.Value ~= root.Parent.Type.Value and vhum.Health > 0 then
if nearest == nil then
nearest = vroot
end
if (root.Position - vroot.Position).magnitude <= maxdist then
maxdist = (root.Position - vroot.Position).magnitude
nearest = vroot
end
end
end
return nearest
end
function policemodule.walktotarget(root, hum, character)
print("go")
local nearesttarget = findnearest(root)
local iseeyou = policemodule.raycastsight(nearesttarget, character)
--body stuff
local animator = hum.Animator
if iseeyou then
if nearesttarget ~= nil then
if nearesttarget.Parent:FindFirstChild("Humanoid").Health > 0 then
if nearesttarget.Parent:FindFirstChild("Type").Value == enemy then
local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath({
AgentCanJump = true,
AgentCanClimb = true,
PathSettings = {
SupportPartialPath = true
}
})
path:ComputeAsync(root.Position, nearesttarget.Position)
local success, errorMessage = pcall(function()
path:ComputeAsync(root.Position, nearesttarget.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(path:GetWaypoints()) do
local waypoints = path:GetWaypoints()
path.Blocked:Connect(function(blockedwaypointindex)
path:Destroy()
policemodule.walktotarget(root, hum, character)
end)
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum:MoveTo(waypoint.Position)
local timeout = hum.MoveToFinished:Wait(2)
if not timeout then
path:Destroy()
hum.Jump = true
policemodule.walktotarget(root, hum, character)
if (root.Position - waypoints[1].Position).magnitude > 1 then
print("yay")
path:Destroy()
policemodule.walktotarget(root, hum, character)
break
end
end
end
else
hum:MoveTo(game.Workspace.Target.Position)
end
end
end
end
else
local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath({
AgentCanJump = true,
AgentCanClimb = true,
PathSettings = {
SupportPartialPath = true
}
})
local waypoints = path:GetWaypoints()
path:ComputeAsync(root.Position, workspace.Target.Position)
local success, errorMessage = pcall(function()
path:ComputeAsync(root.Position, workspace.Target.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(path:GetWaypoints()) do
local waypoints = path:GetWaypoints()
path.Blocked:Connect(function(blockedwaypointindex)
path:Destroy()
policemodule.walktotarget(root, hum, character)
end)
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
if (root.Position - waypoints[1].Position).magnitude < 3 then
print("yay")
path:Destroy()
policemodule.walktotarget(root, hum, character)
break
end
hum:MoveTo(waypoint.Position)
local timeout = hum.MoveToFinished:Wait(2)
if timeout then
for i, v in pairs(path:GetWaypoints()) do
v = nil
end
end
if not timeout then
path:Destroy()
hum.Jump = true
policemodule.walktotarget(root, hum, character)
end
end
elseif path.Status == Enum.PathStatus.NoPath then
path.Blocked:Connect(function(blockedwaypointindex)
path:Destroy()
policemodule.walktotarget(root, hum, character)
end)
end
end
end
function policemodule.raycastsight(nearest, character)
if nearest then
local direction = (nearest.Position - character.Head.Position).Unit * 300
local raycastparams = RaycastParams.new(
character.Head,
character.Torso,
character["Right Arm"],
character["Left Arm"],
character["Right Leg"],
character["Left Leg"]
)
raycastparams.FilterType = Enum.RaycastFilterType.Exclude
local raycast = workspace:Raycast(character.Head.Position, direction, raycastparams)
if raycast then
if raycast.Instance.Parent:FindFirstChild("Humanoid") and raycast.Instance.Parent:FindFirstChild("HumanoidRootPart") and raycast.Instance.Parent:FindFirstChild("Type").Value == enemy then
return true
else
return false
end
end
end
end
return policemodule
