I was trying to make my own pathfinding script which is below
local module = {}
-- functions for movement of agent
-- function to move forward
local function forward(agent)
agent.Position = Vector3.new(agent.Position.X + 1, agent.Position.Y, agent.Position.Z)
end
-- function to move backward
local function backward(agent)
agent.Position = Vector3.new(agent.Position.X - 1, agent.Position.Y, agent.Position.Z)
end
-- function to move right
local function right(agent)
agent.Position = Vector3.new(agent.Position.X, agent.Position.Y,agent.Position.Z +1)
end
-- funtion to move left
local function left(agent)
agent.Position = Vector3.new(agent.Position.X, agent.Position.Y, agent.Position.Z - 1)
end
-- function to move up
local function up(agent)
agent.Position = Vector3.new(agent.Position.X, agent.Position.Y + 1, agent.Position.Z)
end
--function to move down
local function down(agent)
agent.Position = Vector3.new(agent.Position.X, agent.Position.Y - 1, agent.Position.Z)
end
-- function to calculate if closer or not
local function closer(cLoc, goal, pLoc)
-- get distances of old and new locations
local oldDist = (pLoc - goal).magnitude
local newDist = (cLoc - goal).magnitude
-- check which one is smaller
if newDist < oldDist then
return true
else
return false
end
end
-- function to test all moves
local function move(agent, goal, pLoc, Nodes)
if forward(agent) then
if not closer(agent.Position, goal, pLoc) then
agent.Position = pLoc
else
table.insert(Nodes, agent.Position)
return
end
end
if backward(agent) then
if not closer(agent.Position, goal, pLoc) then
agent.Position = pLoc
else
table.insert(Nodes, agent.Position)
return
end
end
if right(agent) then
if not closer(agent.Position, goal, pLoc) then
agent.Position = pLoc
else
table.insert(Nodes, agent.Position)
return
end
end
if left(agent) then
if not closer(agent.Position, goal, pLoc) then
agent.Position = pLoc
else
table.insert(Nodes, agent.Position)
return
end
end
if up(agent) then
if not closer(agent.Position, goal, pLoc) then
agent.Position = pLoc
else
table.insert(Nodes, agent.Position)
return
end
end
if down(agent) then
if not closer(agent.Position, goal, pLoc) then
agent.Position = pLoc
else
table.insert(Nodes, agent.Position)
return
end
end
end
-- function to calculate path
module.calculatePath = function(pos, humanoid)
print("Calculating Path")
-- current location to know where the start position is for starting calculating
local start = humanoid.Position
-- waypoints
local nodes = {}
-- check if position is valid or not
if pos.ClassName ~= Vector3 and pos.ClassName ~= "Part" then
-- generate error and get out of function
error("Position must be Vector3 or part")
return
end
-- create goal position variable
local goal
-- just in case pos is a part
if pos.ClassName == "Part" then
goal = pos.Position
else
goal = pos
end
-- create an invisible agent to find path
local agent = Instance.new("Part", workspace)
-- properties of agent
agent.Name = "Agent"
agent.CanCollide = false
agent.Transparency = 1
agent.Anchored = true
-- set position of agent
agent.Position = start
-- In case of exast
coroutine.yield()
-- loop to find path
while true do
-- get previous location
local pLoc = agent.Position
-- move agent
move(agent, goal, pLoc, nodes)
-- check if solution was found
if agent.Position == goal then
print("Path found")
-- return waypoints
return nodes
end
end
-- no solution found
error("No solution")
return
end
return module
and here is the module script being used
local AI = require(game.ServerScriptService.AI)
local goal = game.Workspace.Part
local path = AI.calculatePath(goal, script.Parent.HumanoidRootPart)
for node in path do
script.Parent.Humanoid:MoveTo(node)
end
Whenever I run the code on a basic block rig it doesn't work. I tried setting the visibility of the agent to 0 to see what the agent is doing and the agent doesn't even move. Any ideas?
