I want to make a pathfinding script that doesn’t break…
problem is every time I try to run the game the bot works but when it starts chasing me it starts jittering back and forth here’s a video on it
I think the problem is here
local function CanseeTarget(target)
local origin = piggy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - piggy.HumanoidRootPart.Position).Unit * 41
local rayparams = RaycastParams.new()
rayparams.FilterType = Enum.RaycastFilterType.Exclude
rayparams.FilterDescendantsInstances = {workspace.Map:GetDescendants(), piggy}
local RaycastResult = workspace:Raycast(origin, direction, rayparams)
if RaycastResult and RaycastResult.Instance then
if RaycastResult.Instance:IsDescendantOf(target) then
return true
end
else
return false
end
end
here is the script
local piggy = script.Parent
local humanoid = script.Parent.Humanoid
local pfs = game:GetService("PathfindingService")
piggy.PrimaryPart:SetNetworkOwner(nil)
local function CanseeTarget(target)
local origin = piggy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - piggy.HumanoidRootPart.Position).Unit * 41
local rayparams = RaycastParams.new()
rayparams.FilterType = Enum.RaycastFilterType.Exclude
rayparams.FilterDescendantsInstances = {workspace.Map:GetDescendants(), piggy}
local RaycastResult = workspace:Raycast(origin, direction, rayparams)
if RaycastResult and RaycastResult.Instance then
if RaycastResult.Instance:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function FindTarget()
local players = game.Players:GetPlayers()
local maxdistance = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (piggy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxdistance and CanseeTarget(target) == true then
nearestTarget = target
maxdistance = distance
end
end
end
return nearestTarget
end
local function attack(target)
local distance = (piggy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 7 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local PlayerDeath = game.ReplicatedStorage.Events.Death
local player = game.Players:GetPlayerFromCharacter(target)
PlayerDeath:FireClient(player, piggy)
target.Humanoid.Health = 0
end
end
local function Getpath(Destination)
local PathParams = {
["AgentHeight"] = 5.2,
["AgentRadius"] = 2,
["AgentCanJump"] = false
}
local path = pfs:CreatePath(PathParams)
path:ComputeAsync(piggy.HumanoidRootPart.Position, Destination.Position)
return path
end
local function Walkto(Destination)
local path = Getpath(Destination)
if path.Status == Enum.PathStatus.Success then
for index, Waypoint in pairs(path:GetWaypoints()) do
local target = FindTarget()
if target and target.Humanoid.Health ~= 0 then
print(target.Name)
attack(target)
break
else
humanoid:MoveTo(Waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(Destination.Position - (piggy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function Patrol()
local Waypoints = workspace.Waypoints:GetChildren()
local RandomNum = math.random(1, #Waypoints)
Walkto(Waypoints[RandomNum])
end
while wait(0.15) do
Patrol()
end
thank you for helping