How to set all of a Table's Indexes to another Table's Indexes?

I am trying to clean a table of its indexes, and set the table to another table. However, it errors.

the table:

local RegDisasterList = {
	'Meteor',
	'Meteor Shower',
	'Destruction Beam'
}

the reset table:

local RegDisasterListReset = {
	'Meteor',
	'Meteor Shower',
	'Destruction Beam'
}

the script to reset the initial table:

if RegDisasterList ~= {} then
			print(RegDisasterList)
			local dis,num = PickFromList(RegDisasterList)
			table.remove(RegDisasterList, num)
			return 'Reg',dis
		else
			RegDisasterList = RegDisasterListReset
			local dis,num = PickFromList(RegDisasterList)
			table.remove(RegDisasterList, num)
			return 'Reg',dis
		end

If anyone can figure this out, It would be appreciated. Thank you!

I’m really confused as to what you’re trying to do, not to mention the fact that you’ve only posted a fraction of your code. Could you please try to disclose your problem further and all of the code required for your program, thanks :smiley:

1 Like

This is nearly identical to the problem I helped resolve yesterday, minus the fact that you haven’t provided any context regarding the error which you should (meaning list the exact error you’re getting):

Part of your problem is that you’re constructing a new table and trying to compare that to an existing table as a means of checking whether or not the table is empty. Two tables are never equal to each other because they point to a different memory address (exceptions apply: __eq metamethod).

Since you’re just working with an array, you should be using the length operator to check if the table is empty. If it is, then you can use table.move to repopulate the outgoing list with elements from the default list. I’d also recommend different variable names so it’s clearer to you what each variable is for: for example, change RegDisasterList to DisasterQueue and RegDisasterListReset to DisasterList.

Anyway, for your code, applying my above feedback you might get something like this:

if #RegDisasterList == 0 then
    table.move(RegDisasterListReset, 1, #RegDisasterListReset, 1, RegDisasterList)
end
-- Please use more descriptive variable names for your own sake. Helps with
-- code readability and maintainability.
local dis, num = PickFromList(RegDisasterList)
table.remove(RegDisasterList, num)
return "Reg", dis

This replaces your whole if statement. There’s no sense in rewriting the same logic based on the fullness of the table so the if statement should only pertain towards repopulating the table if it’s empty. Additionally, tables are passed by reference so you can’t repopulate the table without explicitly inserting elements into it. RegDisasterList = RegDisasterListReset just makes both variables point to the exact same table so changes in one table will be seen in another.

2 Likes

Nevermind guys, I figured it out. Sorry!

If you’ve figured it out and you’re going to mark that post as the solution then please include what you’ve figured out and how you resolved it in the solution post, just as a common courtesy for anyone else experiencing the same problem so they can look through this thread and see how they can resolve it as well. “Nevermind I figured it out” is not a proper solution.