Hello there!
I’m currently creating a satire survival game surviving joke killers, although there is an issue with the pathfinding used for the majority of the killers inside of my game.
As stated before, the killers pathfinding seems to break randomly when they try to chase me if I am on high ground. Here’s a few screenshots with the issue I’m currently dealing with.
As seen in the screenshot, the killers are stuck as they can’t seem to cannot get me. If you aren’t convince yet however, here’s a video. (Skip to around 28-30 second to see the AI break)
External MediaHere’s the script (Using SimplePath since I didn’t feel like scripting the pathfinding for the killers)
--//Tumored
local NPC = script.Parent
local Humanoid = NPC:WaitForChild("Humanoid")
NPC.HumanoidRootPart:SetNetworkOwner(nil) --No hacking
--//Services
local Players = game:GetService("Players")
local PFS = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")
--//Pathfinding Stuff
local AttackingRange = Vector3.new(3,3,3)
local CharacterSize = NPC:GetExtentsSize()
local PathParams = {
AgentRadius = (CharacterSize+CharacterSize)/4,
AgentHeight = CharacterSize.Y,
AgentCanJump = true
}
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--//AI
local function FindNearestTarget()
local players = {}
local nearest = math.huge
local Target = nil
for _, Player in pairs(Players:GetPlayers()) do
local Character = Player.Character or Player.CharacterAdded:Wait()
local Distance = (NPC.HumanoidRootPart.Position - Character.PrimaryPart.Position).Magnitude
if Distance <= nearest then
table.insert(players,{
Magnitude = Distance,
player = Player
})
end
end
for _, Entry in pairs(players) do
local Mag = Entry.Magnitude
local Plr = Entry.player
if Mag <= nearest then
nearest = Mag
Target = Plr
end
end
return Target
end
game:GetService("RunService").Heartbeat:Connect(function()
local Player = FindNearestTarget()
--//Character
local Player_Char = Player.Character or Player.CharacterAdded:Wait()
local Char_Root = Player_Char.PrimaryPart
local Char_Hum = Player_Char:FindFirstChildWhichIsA("Humanoid")
if Char_Hum.Health <= 1 then
return
end
--//Pathfinding
local Goal = Char_Root.Position
local Path = SimplePath.new(NPC)
--Dummy knows to compute path again if something blocks the path
Path.Blocked:Connect(function()
Path:Run(Goal)
end)
--If the position of Goal changes at the next waypoint, compute path again
Path.WaypointReached:Connect(function()
Path:Run(Goal)
end)
--Dummmy knows to compute path again if an error occurs
Path.Error:Connect(function(errorType)
Path:Run(Goal)
end)
Path:Run(Goal)
--[[
local Beginning = NPC.HumanoidRootPart.Position
local Destination = Char_Root.Position
local Path = PFS:CreatePath(PathParams)
Path:ComputeAsync(Beginning,Destination)
if Path.Status == Enum.PathStatus.Success then
local Waypoints = Path:GetWaypoints()
for _, Waypoint in pairs(Waypoints) do
if Waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid:MoveTo(Waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
else
Humanoid:MoveTo(NPC.HumanoidRootPart.Position)
end]]
end)
--//Attacking
--Still not completed yet, so ignore this,
--//Killed
repeat
task.wait()
until Humanoid.PlatformStand == true or Humanoid.Sit == true
Humanoid.Health = 0
game:GetService("Debris"):AddItem(NPC,3)