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

Why my script do not this remove all in the table?

Why my script do not this remove all in the table? Somebody know how to fix my problem? I know I ask a lot questions but I am trying to learn..

--< Creates a table and insert values.
local Maps = {}
local AllMaps = RStorageService.Maps:GetChildren()

for i = 1, 3 do
	local map = AllMaps[math.random(1, #AllMaps)]
	table.insert(Maps, map)
end
print(Maps[1], Maps[2], Maps[3])

OutPut: Asylum, Mansion, Mansion

--< Now remove all values.
for i = 1, #Maps do
	table.remove(Maps, i)
end
print(Maps[1], Maps[2], Maps[3])

OutPut: Asylum, nil, nil

1 comment

0

"Why my script do not this remove all in the table?" You might want to edit that, haha.

1 answer

✓ Accepted answer

Hi NiniBlackJackQc,

For looping through tables, use the in pairs loop and iterate by setting each value as nil. table.remove shifts the elements over to fill in the empty space, which is why all values weren't set as nil.

for i, v in pairs(Maps) do
    Maps[i] = nil
end
print(Maps[1], Maps[2], Maps[3])

Hope this answered your question!

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