ServerScriptService.DisasterSpawner:77: invalid argument #2 to 'random' (interval is empty)?

I’ve got a disaster spawning system and it works… Kind of anyway. For the first time it appears to work, spawning disasters like I want it to, but after that:

ServerScriptService.DisasterSpawner:77: invalid argument #2 to 'random' (interval is empty)  

I’m unsure where I’ve gone wrong here.
Im positive that doing:

math.random(1, #table) 

Is alright, so I have no idea what else could be causing this bug.

FULL SCRIPT:

local function SelectTile()
	local Tiles = workspace["Spleef Map"]:GetChildren()
	return Tiles[math.random(1, #Tiles)]
end
function meteorite()
	local lol_aboutToGetSmashed = {}
	local m = game.ServerStorage.Disasters.Meteorite
	for i = 1, 9, 1 do
		table.insert(lol_aboutToGetSmashed, #lol_aboutToGetSmashed+1, SelectTile())
	end
	for i, v in pairs(lol_aboutToGetSmashed) do
		local clone = m:Clone()
		clone.Meteorite.CFrame = v.CFrame + Vector3.new(0, 10, 0)
		clone.Metorite.Anchored = false
	end
	
end
function Crumble()
	local toCrumble = {}
	for i = 1, 9, 1 do
		table.insert(toCrumble, #toCrumble+1, SelectTile())
	end
	for i, v in pairs(toCrumble) do
		delay(5, function()
			v:Destroy()
		end)
	end
end

function Bomb()
	local clone = game.ServerStorage.Disasters.Bomb:Clone()
	clone.Parent = workspace
	local tile = SelectTile()
	clone.Handle.CFrame = tile.CFrame + Vector3.new(1)
	clone.Handle.Anchored = true
	local Explosion = Instance.new("Explosion", tile)
	tile:Destroy()
	clone:Destroy()
end

function UFO()
	local clone = game.ServerStorage.Disasters.UFO:Clone()
	clone.Parent = workspace
	local chosenTile = SelectTile()
	clone.PrimaryPart.CFrame = chosenTile.CFrame + Vector3.new(0,10,0)
	clone.PrimaryPart.Anchored = true
	local Explosion = Instance.new("Explosion", chosenTile)
	chosenTile:Destroy()
	clone:Destroy()
end

function GearThem()
	local Players = game.Players:GetPlayers()
	for i, v in pairs(Players) do
		local gearFolder = game:GetService("ServerStorage").GearFolder
		local backpack = v.Backpack
		if backpack then
			local chosenToolClone = gearFolder:GetChildren()[math.random(1,#gearFolder:GetChildren())]
			chosenToolClone.Parent = backpack
		end
	end
end

function FIRE()
	local chosenTile = SelectTile()
	local fire = Instance.new("Fire")
	fire.Parent = chosenTile
	fire.Size = 20
	delay(2, function()
		chosenTile:Destroy()
	end)
end
game.ServerStorage.DisasterEvent.Event:Connect(function(number, bool)
	local tbl_disasters = {GearThem(), UFO(), Bomb(), FIRE(), Crumble()}
	local chosenDisasters = {}
	for i  = 1, number do
		table.insert(chosenDisasters, #chosenDisasters+1, tbl_disasters[math.random(1, #tbl_disasters)])
	end
	while bool do
		while wait(2) do
			for i, v in pairs(chosenDisasters) do
				chosenDisasters[i]()
			end
		end

	end

end)

Line that errors:

		table.insert(chosenDisasters, #chosenDisasters+1, tbl_disasters[math.random(1, #tbl_disasters)])

help much appreciated !

1 Like

That error is occuring because #tbl_disasters is appearantly 0. As I’m looking at your functions such as UFO(), ... I see that you are not returning anything. There will be the issue. You could store the disasters as strings and then call the function of disaster that has been chosen. When you do this, you actually run the functions.

1 Like

I bet he can even get away with just not calling the functions with ()

local tbl_disasters = {GearThem, UFO, Bomb, FIRE, Crumble}

1 Like

That is true! Thank you for the message! I haven’t seen this ever before, but it’s a cool thing to know. Appearently I’m missing some basic things :joy:.

1 Like

I will try both yours and @MegaBorecYT’s methods

1 Like

Definitely not the approach I would take, but you can do some crazy things with tables in Lua so might as well lol. For example, this is valid:

local tab = {}
tab[workspace.Part] = “Why”
print(tab[workspace.Part])

1 Like

Nvrmind still errors, I didn’t think it was going to

I’ll try @MegaBorecYT’s way too

Was the error still the same thing? I think the way that @Barothoth mentioned should work great and it is actually better way than mine.

1 Like

Yep, still the same thing. I’m implementing ur idea rn

Code I’m using:

local function SelectTile()
	local Tiles = workspace["Spleef Map"]:GetChildren()
	return Tiles[math.random(1, #Tiles)]
end
function meteorite()
	local lol_aboutToGetSmashed = {}
	local m = game.ServerStorage.Disasters.Meteorite
	for i = 1, 9, 1 do
		table.insert(lol_aboutToGetSmashed, #lol_aboutToGetSmashed+1, SelectTile())
	end
	for i, v in pairs(lol_aboutToGetSmashed) do
		local clone = m:Clone()
		clone.Meteorite.CFrame = v.CFrame + Vector3.new(0, 10, 0)
		clone.Metorite.Anchored = false
	end
	
end
function Crumble()
	local toCrumble = {}
	for i = 1, 9, 1 do
		table.insert(toCrumble, #toCrumble+1, SelectTile())
	end
	for i, v in pairs(toCrumble) do
		delay(5, function()
			v:Destroy()
		end)
	end
end

function Bomb()
	local clone = game.ServerStorage.Disasters.Bomb:Clone()
	clone.Parent = workspace
	local tile = SelectTile()
	clone.Handle.CFrame = tile.CFrame + Vector3.new(1)
	clone.Handle.Anchored = true
	local Explosion = Instance.new("Explosion", tile)
	tile:Destroy()
	clone:Destroy()
end

function UFO()
	local clone = game.ServerStorage.Disasters.UFO:Clone()
	clone.Parent = workspace
	local chosenTile = SelectTile()
	clone.PrimaryPart.CFrame = chosenTile.CFrame + Vector3.new(0,10,0)
	clone.PrimaryPart.Anchored = true
	local Explosion = Instance.new("Explosion", chosenTile)
	chosenTile:Destroy()
	clone:Destroy()
end

function GearThem()
	local Players = game.Players:GetPlayers()
	for i, v in pairs(Players) do
		local gearFolder = game:GetService("ServerStorage").GearFolder
		local backpack = v.Backpack
		if backpack then
			local chosenToolClone = gearFolder:GetChildren()[math.random(1,#gearFolder:GetChildren())]
			chosenToolClone.Parent = backpack
		end
	end
end

function FIRE()
	local chosenTile = SelectTile()
	local fire = Instance.new("Fire")
	fire.Parent = chosenTile
	fire.Size = 20
	delay(2, function()
		chosenTile:Destroy()
	end)
end
game.ServerStorage.DisasterEvent.Event:Connect(function(number, bool)
	local tbl_disasters = {"GearThem", "UFO", "Bomb", "FIRE", "Crumble"}
	local chosenDisasters = {}
	for i  = 1, number do
		table.insert(chosenDisasters, #chosenDisasters+1, tbl_disasters[math.random(1, #tbl_disasters)])
	end
	while bool do
		while wait(10) do
			for i, v in pairs(chosenDisasters) do
				if v == "GearThem" then
					GearThem()
				elseif v == "UFO" then
					UFO()
				elseif v == "Bomb" then
					Bomb()
				elseif v == "FIRE" then
					FIRE()
				elseif v == "Crumble" then
					Crumble()
					
				end
			end
		end

	end

end)

Oddly enough, your method actually worked! Completely erased the error that was bugging me for so long

1 Like