How would I create a node-based travel system?

This is what I’ve got so far:

local tweenService = game:GetService("TweenService")
local nodes = { }
for i, v in ipairs(game.Workspace.Nodes:GetChildren()) do
	table.insert(nodes, #nodes + 1, v)
end
local lastNode = nodes[#nodes]
local firstNode = nodes[1]
local lastNodePosition = lastNode.Position.X
local beforeLastNodePosition = nodes[#nodes - 1].Position.X
local distance = lastNodePosition - beforeLastNodePosition
local info = TweenInfo.new(distance.X / 5)

I’m not much of a scripter and I know this probably isn’t correct, but I can’t figure it out at all lol.

EDIT: to clarify, I want the car to go the same speed from node A to node B as it would from Node B to node C, even if node B to C is 200 studs longer

Here is the small formula that you might require, i don’t know if this is correct:

local StudsPerSecond = 1 --// Unecessary to explain?
local Distance = (Position1.Position - Position2.Position).Magnitude --// Gets the Magnitude from a position to another, to clarify, magnitude converts a vector to one number.
local TimeRequired = StudsPerSecond * Distance --// An example: if Pos2 is 1 stud far from Pos1, then TimeRequired would multiply StudsPerSecond * Distance, in this case, TimeRequired would be 1, because 1 * 1 = 1.

I hope this helps :slight_smile: (School didn’t help me, in any way lol)

Ok. Thanks. One more thing - how do I find the position of a value “v” in a table? Like, if I loop through the table, how do I find the position of “v” in the table?

Eh, that’s easy:

v.Position

To prevent that confusion, name v “Node” instead :+1:.
EDIT: Sorry, i read very quick, you might want to insert the object in the table instead, or maybe, just maybe, i don’t know if this is going to help, but you could do something like this:

nodes[#nodes + 1] = v

But i think how you’re doing it is ok? I have never tried to use the third argument from table.insert.

Oops, let me clarify - the position of “v” in the table. I.e. nodes[–]

You would do table.find(game.Workspace.Nodes:GetChildren(), v), this returns the position of it in the children table

Would something like this work?

local tweenService = game:GetService("TweenService")

local nodes = { }

for i, v in ipairs(game.Workspace.Nodes:GetChildren()) do

table.insert(nodes, #nodes + 1, v)

end

local lastNode = nodes[#nodes]

local firstNode = nodes[1]

for i, v in ipairs(nodes) do

local pos = nodes[table.find(nodes, v)]

local pos2 = nodes[table.find(nodes, v) + 1]

local StudsPerSecond = 0.1 --// Unecessary to explain?

local Distance = (pos.Position - pos2.Position).Magnitude --// Gets the Magnitude from a position to another, to clarify, magnitude converts a vector to one number.

local TimeRequired = StudsPerSecond * Distance --// An example: if Pos2 is 1 stud far from Pos1, then TimeRequired would multiply StudsPerSecond * Distance, in this case, TimeRequired would be 1, because 1 * 1 = 1.

local change = {Position = Vector3.new(v.Position.X, v.Position.Y, v.Position.Z), Orientation = Vector3.new(v.Orientation.X, v.Orientation.Y, v.Orientation.Z)}

local info = TweenInfo.new(TimeRequired)

local tween = tweenService:Create(script.Parent.Parent, info, change)

tween:play()

tween.Completed:Wait()

end

Yes it would. Probably (30 characters)

Instead of having to waste yours other people’s time asking if it will work, you can easily test it yourself.

By the way this is redundant:

You can just do v.Position and v.Orientation.

By the way for this case the second optional argument (sounds weird doesn’t it) is unnecessary as well. When table.insert is called with only two arguments the second one is appended to the end automatically.

1 Like

Actually table.insert() has 3 arguments which are the table, position and value.

But yeah if someone uses the value as second argument then it will put the value inside the table just like how J_oystick is probably wanting (Putting in the next position)

1 Like

I am aware.

But OP is using it to append it to the very end; in this case adding it yourself is redundant and you should let the overload do it for you.

You can use @Crazyman32’s plugin, i’m not sure how to use it myself I just found it today, but maybe that can help you
roblox.com/library/645414345/Node-Map
And it’s free :smile:

2 Likes

I’ll probably end up doing this.
One question though:

path = Pathfinder:FindPath(startPosition, endPosition)

For this, what would the startPosition and endPosition be if I have a lot of nodes?

2 Likes