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

Is there a way to condense this code?

I'm creating a multiplayer game that allows multiple characters to be controlled by the same keyboard (Super Smash Brothers, essentially). As a result, I have the same set of variables for each character, and my code looks like this:

player1 = {
	idle = true;
	jumping = false;
	-- Et cetera
}

player2 = {
	idle = true;
	jumping = false;
	-- You get the idea, hopefully.
}

How might I be able to create the same effect without taking up a ton of script space? I would assume that for loops are the way to go, but is there any other way of doing it?

2 answers

✓ Accepted answer

You could do like gskw said or also do

local table1 = { 2, 1 }
local table2 = { unpack(table1) }

which is essentially the same thing but stands in a single line.

Note however that if table1 contains non-numerical indexes, they will be lost.

You can do this using a very basic for loop, i.e.

local table1 = {a = 2, b =1};
local table2 = {};

for key, val in next, table1 do -- For every key in table 1
	table2[key] = val; -- Copy the key-value pair over to table 2
end

2 comments

0

Can you not just do table2 = table1? Same effect but doesn't need to loop through every value?

2

That would make table2 a reference to table1 and not a copy.

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