Script timeout: exhausted allowed execution time

I’m trying to implement a rarity system in my game, however, I’ve run through an issue that I can’t seem to figure out…

The problematic script:

function Roll()
	local CurrentBestRarity = "Rock"

	for RarityTier, Chance in OreArray do
		local NewRoll = Rand:NextNumber()
		if NewRoll < Chance then
			if OreArray[CurrentBestRarity] > Chance then
				CurrentBestRarity = RarityTier
			end
		end
	end
	return CurrentBestRarity
end


local spawnBricks = game.Workspace.Spawns:GetChildren()

local InitializeOre = 6

local function OreSpawn()

	local OresToSpawn = {}


	for i = 0, InitializeOre, 1 do
		task.spawn(function()
			local result = Roll()

			table.insert(OresToSpawn, result)

		end)
	end

	for Index, Ore in OresToSpawn do



		local Index : number = math.random(#spawnBricks)
		local Chosen : Part = spawnBricks[Index]
		local FoundLocation = Spawns:FindFirstChild(Chosen.Name)

		if Chosen.Occupied.Value == true then
			repeat
				Index = math.random(#spawnBricks)
				Chosen = spawnBricks[Index]
				FoundLocation = Spawns:FindFirstChild(Chosen.Name)
			until Chosen.Occupied.Value == false
		end


		if Ore == "Rock" then
			local rockClone = Ores[1]:Clone()
			rockClone.Parent = FoundLocation
			rockClone:PivotTo(FoundLocation.CFrame)
			rockClone.Parent.Occupied.Value = true
		elseif Ore == "IronOre" then
			local ironClone = Ores[2]:Clone()
			ironClone.Parent = FoundLocation
			ironClone:PivotTo(FoundLocation.CFrame)
			ironClone.Parent.Occupied.Value = true
		elseif Ore == "GoldOre" then
			local goldClone = Ores[3]
			goldClone.Parent = FoundLocation
			goldClone:PivotTo(FoundLocation.CFrame)
			goldClone.Parent.Occupied.Value = true
		elseif Ore == "AzureOre" then
			local azureClone = Ores[4]:Clone()
			azureClone.Parent = FoundLocation
			azureClone:PivotTo(FoundLocation.CFrame)
			azureClone.Parent.Occupied.Value = true
		end
	end
end


Players.PlayerAdded:Connect(OreSpawn)

The error:
image

1 Like

The error exhausted allowed execution time usually triggers when you have a loop that to quickly. It is likely that your for loop never stops, triggering this issue. To fix this issue, you can insert a wait() into your loop to help slow it down to stop the error from triggering.

1 Like

Aha! I forgot to include a wait()! Thank you for pointing that out.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.