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
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!