I am making a game that is very similar to [The Rake] (https://www.roblox.com/games/2413927524/THE-RAKE-Classic-Edition-v1-1-1c)
You're put in a map that has buildings and other obstacles littered around it. There is a monster that is supposed to chase you throughout this map. I have used basic things like MoveTo(), but obviously that doesn't work very well because the monster can't get around obstacles. So I tried different ways to do this using pathfinding, but I still can't get it just right.
This is my code as of right now. If it looks cluttered it's because I've constantly been changing it to try to fit my needs.
local runSpeed = 28
local pathService = game:GetService("PathfindingService")
local humanoid = script.Parent.Humanoid
humanoid.WalkSpeed = runSpeed
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 800
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
local path
while true do
wait(0.5)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
path = pathService:CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(waypoints) do
local part = Instance.new("Part", workspace)
part.Anchored = true
part.CanCollide = false
part.Material = Enum.Material.Neon
part.Position = v.Position
part.Size = Vector3.new(0.6,0.6,0.6)
part.Shape = "Ball"
humanoid:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
end
end
end
end
Like I said it's pretty cluttered. What this does right now is it makes a path over and over towards the closest player. But instead of following this path correctly, the monster goes straight to the waypoint at the end of the path. Also, the monster is supposed to jump over obstacles in the path, but fails to do so. I've been at this all day and haven't been able to come to a solution. Keep in mind most of this is bits and pieces from videos, forums, the wiki etc. I am not an experienced scripter so most of this is pretty difficult for me.
Please, if you can, explain and show me how to fix this and how to do it efficiently, if possible. Thanks.

3 comments
ALSO MAKE A VALUE
there are alot of pathfinding models, i would just use the scripts in those
I did try that but none of them really worked well