Hello! I’m trying to make a horror game but, my AI just walks toward the player, ignoring objects in its path running into them as it goes.
local PathfindingService = game:GetService("PathfindingService")
local runService = game:GetService("RunService")
local longPos = math.huge
local targetPLR = nil
runService.Heartbeat:Connect(function()
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character then
local magnitude = script.Parent.HumanoidRootPart.Position.Magnitude - player.Character.HumanoidRootPart.Position.Magnitude
local path = PathfindingService:CreatePath({
AgentRadius = 4,
AgentHeight = 5,
AgentCanJump = true,
WaypointSpacing = 4,
Costs = {
Water = 20
}
})
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
script.Parent.Humanoid:MoveTo(waypoint.Position)
end
end
end
end)
EDIT: I also wanted to say the magnitude will be for a different function.
Hi, I Believe the issue is that when you use Path Finding service it creates mini points, in the workspace and it would move to each point one by one until it reaches its destination.
So whats Happening is that the for loop has finished before the Humanoid has even moved to its first wayPoint, so you gotta wait for the Humanoid to finish moving before you go to the next waypoint in the for loop.
Use this For loop:
for i, waypoint in pairs(waypoints) do
script.Parent.Humanoid:MoveTo(waypoint.Position)
script.Parent.Humanoid.MoveToFinished:wait() -- Waits for the Humanoid To finish Moving.
end
also Set the network owner of the Zombie Root part to the Server, this will get rid of any Jittering.
After You set the magnitude do:
local magnitude = script.Parent.HumanoidRootPart.Position.Magnitude - player.Character.HumanoidRootPart.Position.Magnitude
script.Parent.HumanoidRootPart:SetNetworkOwner(nil)
Hello! Thanks so much for your help! Though I still have one issue with it. It starts spinning in circles when it walks off a curb. Any clue as to why?
Am not sure seems to work for me, maybe another of your script is interfering, if this is your first time working with AI you will face alot more problems.
Also your current AI script logic its not practical and incomplete, you’ll have to use Raycasting for detecting walls and objects and etc I suggest you watch and follow this guide its a very good video for learning about AI: Roblox - Zombie Advanced AI Tutorial (Pathfinding, Custom Animations, Raycasting) - YouTube
He creates an AI which tracks down a target and then successfully gets to that target avoiding all obstacles.