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 make a function completely clear a table?

I'd like to be able to make this function clear out the table, or overwrite it instead of adding onto it. How would I go about that? I couldn't just use table.remove(rolls) could I?

sides = 6
numdice = 1

rolls{}


function rollthedice()
	for i = 1, numdice do
		table.insert(rolls, math.random(1,sides))
	end
end

1 answer

✓ Accepted answer

For many purposes, you can just replace the variable with a new empty table:

rolls = {}

This doesn't actually affect the old object, meaning if you had something like this:

local old_rolls = rolls
rolls = {}
print(#old_rolls) --> 5 or whatever

the ones referencing the old table won't be cleared. Normally you shouldn't have that case.

If you have a list, you can repeatedly table.remove from it:

while #rolls > 0 do
	table.remove(rolls)
end

2 comments

1

whiel -> while

0

oops

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