Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I change objects in a table? [SOLVED]

I need this table to change each position.Y value in a table by 0.5 each time. Such as doing 5, 5.5, 6, 6.5 etc. The problem I'm facing is that the table can't run a 0.5 value, and if I do a for loop inside of a for loop (one editing the table, the other editing the position) it will loop the position changing 13 times.


PathLine1Table = 

{Path.Pathkey5, 
Path.Pathkey6, 
Path.Pathkey7, 
Path.Pathkey8, 
Path.Pathkey9, 
Path.Pathkey10, 
Path.Pathkey11, 
Path.Pathkey12, 
Path.Pathkey13}
		
for i = 5, #PathLine1Table, 0.5 do
PathLine1Table[i].Position = Vector3.new(-2.5, i, 0)```
end

3 comments

0

I'm confused as to what you're doing. If you increment by .5 then PathLine1Table[i] could potentially be nil.. Are you just trying to move them all at once in increments of .5?

0

You could determine whether or not PathLine1Table[i] is nil prior to attempting to manipilate it's Position. That will make it so the code doesn't error.

0

How would I determine if it is nil or not? Also, I'm trying to move them in increments of 0.5.

1 answer

✓ Accepted answer

You're mixing up indexing the table with changing the position of the path. On line 13, PathLine1Table has values at indices 1 through 9

-- Create a table using a for loop since all your entries have the same name with one number different:
local paths = {}
for i = 5, 13 do -- I'm using 5 and 13 here because you initialized your table with Pathkey5 to Pathkey13
	table.insert(paths, Path["Pathkey" .. i]) -- Path["Pathkey5"] is the same as Path.Pathkey5, so we're constructing the key based on 'i' here
end

-- If needed, put the code you want in a function so you can call it more than once
function MoveAllPathsUp(amount) -- amount should be the amount to move up
	local amountVector3 = Vector3.new(0, amount, 0)
	for i = 1, #paths do
		paths[i].CFrame = paths[i].CFrame + amountVector3
	end
end
MovePathsUp(0.5)

-- Or if you want the first path at height 5, the next one at height 5.5, etc (assuming you want them all at x=-2.5 and z=0)
for i = 1, #paths do
	paths[i].Position = Vector3.new(-2.5, i * 0.5 + 4.5, 0) -- We create a formula here based on 'i', rather than trying to make 'i' the value we want to begin with.
end

1 comment

0

Thanks, it works well and I appreciate the help.

This discussion is preserved as it was. New answers, comments, and votes are closed.