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

Can you make a variable with two bits of data?

local l1 = script.Parent.o
local l2 = script.Parent.t
local l3 = script.Parent.th
local l4 = script.Parent.f
local l5 = script.Parent.fi

repeat
	for n = 1, 5 do

	end
until script.Parent.on.Value == 0

So I wanted to simplify my code using a for loop. Is it possible to have the n in the for loop combine with a l at the start to get the local variables at the top of the code. So I could repeat the code 5 times where it would apply the properties to l1 l2 l3 l4 and l5.

Apologies if I haven't explained very well.

1 answer

✓ Accepted answer
local array = {script.Parent.o, script.Parent.t, script.Parent.th, script.Parent.f, script.Parent.fi}

repeat
	for i, v in pairs(array) do
		--Something
	end
	wait()
until script.Parent.on.Value == 0

You can place all of these variables into a table and iterate over them in order using the generic for loop.

i is the argument (key = [#]) and v is the value in the table (any of these 5 variables)

Also, repeat is a loop, so you'll need a wait() somewhere to prevent crashing

2 comments

0

Thank you very much. I'll probably use arrays in the future alongside some other scripts. And thanks for the reminder of the wait()

0

I strongly suggest using an event for this rather than a loop.

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