Repeat Function causes script to timeout

hi sooo
im trying to make an airdrop system, where a drop has a loot table of certain items, each item has a max amount that can be in drop at once, and a chance to be in the drop.

so, the thing is, i made this system to do these random loot table shenanigans, in theory it should be working.

in practice, i get a Script Timeout: exhausted allowed execution time error, and i have no idea how to fix it

the problem seems to be lying in the repeat function-thing

local function AddItem()
	for i, tool in ipairs(AvailableItems) do
		if #CurrentItems < MaxItems then

			local TotalPercent = 100
			for i = 1, AvailableItems[i][3], 1 do
				if math.random(AvailableItems[i][2],TotalPercent) <= AvailableItems[i][2] and #CurrentItems < MaxItems then -- checks random chance
					local AmountOfItems = 0
					for num, repeats in ipairs(CurrentItems) do -- checks if amount of a certain item type in drop is less than its allowed value
						if repeats == AvailableItems[i][1] then
							AmountOfItems += 1
						end
					end
					if AmountOfItems > AvailableItems[i][3] then
						table.insert(CurrentItems,AvailableItems[i][1])
					end
				end
				TotalPercent -= AvailableItems[i][2] -- makes random chance bigger on a reroll
			end

		end
	end
end

local function CreateToolList()
	if Tools ~= nil and Tools:GetChildren() then
		if AvailableItems[1] == nil then
				for i, tool in pairs(Tools:GetChildren()) do -- creates a table of all items available for drops
					if tool:GetAttribute("ChanceInDrop") then
						local NewTable = {}
						NewTable[1] = tool
						NewTable[2] = tool:GetAttribute("ChanceInDrop") -- chance of item type in drop
						NewTable[3] = tool:GetAttribute("MaxInDrop") -- max amount of item type in drop
						table.insert(AvailableItems,NewTable)
					end
				end
		end
		
		repeat AddItem() until #CurrentItems == MaxItems -- TIMEOUT HAPPENS HERE
		for i, v in pairs(CurrentItems) do
			local Value = Instance.new("ObjectValue",script.Airdrop.Tools)
			Value.Value = v
			Value.Name = v.Name
		end
		CurrentItems = {}
	end
end

1 Like

Add a task.wait() to the end of the function.

1 Like

Try do this instead, I haven’t read the entire code but #CurrentItems >= MaxItems