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

How to clear all values in a table?

Hey all! So, I'm just wondering if there's a simple way of clearing the values of a table. I believe you can use a for loop to go through each value, and remove it, but is there a simpler way? Any advice? Thanks!

2 answers

✓ Accepted answer

If you just need to replace a table with an empty one, you can simply do that:

local a = {1, 2, 3}
print(#a) -- 3
a = {}
print(#a) -- 0

However, this won't always be what you want:

local a = {1, 2, 3}
local b = a
print(#b) -- 3
a = {}
print(#b) -- 3 (still)

In general, you should avoid doing this sort of thing -- saving references to things you take in is bad because it can be confusing (like in the above).

You have no choice but to change the actual value, which you need to do with a loop. Luckily, it's not hard at all to remove all of the values from a table:

for index in pairs(tab)
	tab[index] = nil
end
local table = {1, 2346, 6236246, "stuff", true}
table = {}

You can just overwrite the table with a blank table :)

2 comments

0

Wow lol. I feel so dumb how I didn't realize that. Thanks xD!

0

Actually, I think Blue should've gotten the accept thing, He had multiple ways of doing it and he said there is a slight problem with doing "table = {}".

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