How to make Table reset when all numbers are picked?

I’m trying to make a script where it will pick random disasters, I made it so the script won’t pick already chosen numbers. The script works but when everything on the table is already picked it won’t reset back. Here’s my script.

for i=1,#disasters do
local item = game.ReplicatedStorage:findFirstChild(disasters[i])
if item ~= nil then
item.Parent = nil
table.insert(items, item)
else
print(“No Disaster Was Found”)
end
end

function chooseDisaster()
return table.remove(items, math.random(1, #items))
end

items = {}

maybe like this if you want to reset/clean it

1 Like

I think a second list would be best for this. This way the first list is the options to chose from and the second is a copy of the list that you remove items from.

Heres an example.

-- Create Or Reset CheckList
function ResetCheckList()
   local CheckList = {}
   for i = 1 ,#disasters do
      table.insert(CheckList,disasters[i])
   end
end

-- Pick new
function SelectDisaster()
   local RandomDisaster = CheckList[Math,Random]
   Checklist[RandomDisaster] = nil
   return RandomDisaster
end

On a side note you should create the local variables outside of the function so it reuses the same variable as opposed to creating a new one every time.

1 Like

I see how the script would work now, but how can I make it so the script will know when the table is empty and it can activate the reset function.

well you can just call it after you clear it or listen for when you cant index the next step over

if not next(items) then
  --empty
end

Have 2 tables. 1 table with all the disasters and 1 with the current disasters. Then whenever you call a disaster, check if the second table is empty. If it is, then loop through the first table and insert the disasters into Table 2.

local Maps = workspace.Maps:GetChildren()
local ChosenMap = Maps[math.random(1,#Maps)]
print(ChosenMap.Name)
I dont know what your doing so configure it