I want to know how to make a simple algorithm without using pathfinding or neural network, or A* pathfinding, module.
I want to like take a part, and it prints (‘found part’) when in within 3 studs, but how do i make so it only find its target by using green nodes? Anything but not the things above.
A* is a really great combination of simplicity, completeness/correctness and speed. So… why not A*?
Roblox internally already uses A* pathfinding if I’m correct.
Unless you have a very good reason to rewrite it from scratch I’d just use that one.
Roblox internal pathfinding is also written in C++ which makes it about 10 - 100x faster than doing a Roblox Lua implementation of it.
simplicity? i don’t even know how to even use it.
A really bad implementation of what you’re after:
-- Iterate through the objects in the workspace
for k : number, v : Instance in pairs(game.Workspace:GetDescendants()) do
local tbl = {}
-- If the object is within 3 studs...
if math.abs((v.Position - car.Position).Magnitude) <= 3 then
-- Append the object to a table
table.insert(tbl, v)
end
end
As others have said, pathfinding is a way better idea, and I suggest using that over the method I’ve written. It’s quicker, simpler and is more intuitive in its calculations, whereas the above is written in Luau which takes longer to compile & execute.
This should help you learn pathfinding.
This might be the solution, now how do i make it so like after you pass node1, then node2 is the target.
and if node2 then node3, you know what i mean, but how.
As I mentioned before, this is not a good way of going about things.
It’s far better to learn pathfinding, and other integrated ROBLOX services and conventions.
My method differs from regular pathfinding because of your node system. Conventional pathfinding will only generate nodes (waypoints) based on one path, rather than in a grid system like you’ve created. This means that you can both abstract pathfinding to other functions (the essence of OOP) and don’t have to account for nodes that are not required to go from point A to point B.
However, to push the boat out and give you a start if you’re insistent on using my approach:
for k : number, v : Instance in pairs(tbl) do
-- Move to the next waypoint in the table
npc:MoveTo(v.Position)
-- Remove the waypoint the NPC just moved to from the table
table.remove(v)
end
This method will require you to use ordered tables. Tables in Lua are not ordered by default. Using this approach without an ordered table will cause your NPC to move to seemingly random nodes instantaneously.
I can’t stress enough how much of a better habit it is to use the integrated pathfinding ROBLOX provides, it’s simpler and straightforward with documentation designed to help developers.
Good luck.