ServerScriptService.MainScript:38: invalid argument #2 to 'random' (interval is empty)

It prints this error for some reason. Whats the issue?

Script:

--!optimize 2

local faces = workspace:GetAttribute("Faces")
local randomSizes = workspace:GetAttribute("RandomSizes")
local randomMaterials = workspace:GetAttribute("RandomMaterials")
local randomTextures = workspace:GetAttribute("RandomTextures")
local amount = workspace:GetAttribute("Marbles") - 1
local randomEventsEnabled = workspace:GetAttribute("RandomEvents")
local time = 0.00

local randomEvents = {
	["LowGravity"] = { -- low gravity
		Time = math.random(10,20),
		Sky = game.ReplicatedStorage.Events.LowGravity,
		Gravity = 20
	},
	["MarbleExplosion"] = { -- all the marbles will explode
		BlastRadius = 3000000,
		BlastPressure = 1000000
	}
}

local function limitTo1Dp(num: number): string
	return string.format("%.2f", num)
end


task.spawn(function()
	game:GetService("RunService").Heartbeat:Connect(function(delta)
		time += delta
	end)
end)

task.spawn(function()
	if randomEventsEnabled then
		while task.wait(math.random(5,20)) do
			print("HAPPENED")
			local randomEvent = randomEvents[math.random(1,#randomEvents)]
			if randomEvent == "LowGravity" then
				task.spawn(function()
					local ogGrav = game.Workspace.Gravity
					game.Workspace.Gravity = randomEvents.LowGravity.Gravity
					randomEvents.LowGravity.Sky:Clone().Parent = game.Lighting
					game.Lighting:FindFirstChild("Sky").Parent = game.ReplicatedStorage.Events.LowGravity
					task.wait(randomEvents.LowGravity.Time)
					randomEvents.LowGravity.Sky:Destroy()
					game.ReplicatedStorage.Events:FindFirstChild("Sky").Parent = game.Lighting
					game.Workspace.Gravity = ogGrav
				end)
			elseif randomEvent == "MarbleExplosion" then
				task.spawn(function()
					for i, v in pairs(workspace:FindFirstChild("balls"):GetChildren()) do
						local explosion = Instance.new("Explosion", v)
						explosion.Position = v.Position
						explosion.BlastRadius = randomEvents.MarbleExplosion.BlastRadius
						explosion.BlastPressure = randomEvents.MarbleExplosion.BlastPressure
					end
				end)
			end
		end
	end
end)

local Materials = {
	Enum.Material.Air,
	Enum.Material.Asphalt,
	Enum.Material.Basalt,
	Enum.Material.Brick,
	Enum.Material.Cobblestone,
	Enum.Material.Concrete,
	Enum.Material.CorrodedMetal,
	Enum.Material.CrackedLava,
	Enum.Material.DiamondPlate,
	Enum.Material.Fabric,
	Enum.Material.Foil,
	Enum.Material.Glacier,
	Enum.Material.Granite,
	Enum.Material.Grass,
	Enum.Material.Ground,
	Enum.Material.Ice,
	Enum.Material.LeafyGrass,
	Enum.Material.Limestone,
	Enum.Material.Marble,
	Enum.Material.Metal,
	Enum.Material.Mud,
	Enum.Material.Neon,
	Enum.Material.Pavement,
	Enum.Material.Pebble,
	Enum.Material.Plastic,
	Enum.Material.Rock,
	Enum.Material.Salt,
	Enum.Material.Sand,
	Enum.Material.Sandstone,
	Enum.Material.Slate,
	Enum.Material.SmoothPlastic,
	Enum.Material.Snow,
	Enum.Material.Wood,
	Enum.Material.WoodPlanks
}


local textureList = {
	Enum.SurfaceType.Studs,
	Enum.SurfaceType.Weld,
	Enum.SurfaceType.Smooth,
	Enum.SurfaceType.Glue,
	Enum.SurfaceType.Universal,
	Enum.SurfaceType.Inlet
}

local Names = {
	"Aiden","Blaze","Carter","Dexter","Eli","Finn","Grayson","Hudson","Isaac","Jaxon",
	"Kai","Leo","Mason","Nolan","Owen","Parker","Quinn","Ryder","Sawyer","Tyler",
	"Uriah","Victor","Wyatt","Xander","Yusuf","Zane","Arlo","Brody","Caleb","Dylan",
	"Emmett","Felix","Gavin","Henry","Ivan","Jude","Knox","Landon","Micah","Noah",
	"Orion","Preston","Reed","Silas","Tristan","Ulysses","Vaughn","Wesley","Zachary","Asher",

	"Bennett","Colton","Dominic","Ezekiel","Franklin","George","Harrison","James","Kyle","Logan",
	"Malachi","Nathan","Oscar","Phoenix","Roman","Sebastian","Theo","Vincent","William","Xavier",
	"Yosef","Zion","Axel","Bryson","Corbin","Declan","Easton","Finnegan","Garrett","Holden",
	"Ian","Jonah","Kieran","Luca","Miles","Nico","Oliver","Pierce","Quincy","Rowan",
	"Spencer","Tobias","Ulric","Weston","Zeke","Aaron","Brandon","Chase","Daniel","Evan",

	"Flynn","Gianni","Hector","Ismael","Jasper","Kaden","Leon","Marco","Neil","Otis",
	"Paxton","Rafael","Santino","Tanner","Uriel","Valen","Walker","Zander","Alfred","Beckett",
	"Cedric","Damian","Ellis","Forest","Gideon","Harvey","Ira","Julian","Keaton","Lawson",
	"Matteo","Nash","Onyx","Porter","Remy","Sterling","Titan","Vito","Warren","Zayden",

	"Atlas","Bodhi","Cyrus","Dante","Elijah","Fletcher","Greyson","Hugo","Indigo","Jett",
	"Kellan","Lyle","Maddox","Nehemiah","Oakley","Pablo","Ronan","Soren","Talon","Vance", "Ryan", "O'Neil"
}

for i = 1, amount do
	local marble = workspace:FindFirstChild("balls"):FindFirstChild("Part"):Clone()
	marble.Parent = workspace:FindFirstChild("balls")
	local x = workspace:FindFirstChild("Lobby"):FindFirstChild("Area").Size.X * 0.5
	local y = workspace:FindFirstChild("Lobby"):FindFirstChild("Area").Size.Y * 0.5
	local z =workspace:FindFirstChild("Lobby"):FindFirstChild("Area").Size.Z * 0.5
	marble.Position = workspace:FindFirstChild("Lobby"):FindFirstChild("Area").Position + Vector3.new(math.random(0 - x, x), math.random(0 - y, y), math.random(0 - z,z))
end

task.wait(1)

for i,v in pairs(workspace:FindFirstChild("balls"):GetChildren()) do
	if v and v:IsA("BasePart") then
		v.Anchored = false
		v.Massless = true
		v:SetNetworkOwnershipAuto()
		local billboard = Instance.new("BillboardGui", v)
		billboard.SizeOffset = Vector2.new(0,1.25)
		billboard.Size = UDim2.new(8,0,8,0)
		local imagelabel = Instance.new("TextLabel", billboard)
		imagelabel.Size = UDim2.new(1,0,1,0)
		imagelabel.TextScaled = true
		imagelabel.TextColor3 = Color3.fromRGB(255,255,255)
		imagelabel.BackgroundTransparency = 1
		local randomyes = math.random(1, #Names)
		local yesyes = Names[randomyes]
		imagelabel.Text = yesyes.." #"..i.." Time: "..limitTo1Dp(time)
		task.spawn(function()
			game:GetService("RunService").Heartbeat:Connect(function(delta)
				imagelabel.Text = yesyes.." #"..i.." Time: "..limitTo1Dp(time)
			end)
		end)
		task.spawn(function()
			while true do
				task.wait(math.random(20,120))
				task.spawn(function()
					if faces then
						if v:FindFirstChildOfClass("Decal") then
							v:FindFirstChildOfClass("Decal").Texture = "rbxassetid://15603094408"
							task.wait(1)
							if v:FindFirstChildOfClass("Decal") then
								v:FindFirstChildOfClass("Decal").Texture = "rbxassetid://925874207"
							end
						end
					end
				end)
				local ex = Instance.new("Explosion", v)
				ex.DestroyJointRadiusPercent = 0
				ex.Position = v.Position
				ex.BlastRadius = 3000000
				ex.BlastPressure = 1000000

			end
		end)
		if randomMaterials then
			if randomTextures then
				warn("Random Materials and Random Textures were both enabled! Please only enable one of them next time.")
				randomTextures = false
				v.Material = Materials[math.random(1,#Materials)]
			end
			v.Material = Materials[math.random(1,#Materials)]
		end
		if randomSizes then
			local randomSizeYes = math.random(5,14)
			v.Size = Vector3.new(randomSizeYes, randomSizeYes, randomSizeYes)
		end
		if randomTextures then
			local randomTexturesYes = math.random(1, #textureList)

			v.TopSurface = textureList[randomTexturesYes]
			v.BottomSurface = textureList[randomTexturesYes]
			v.LeftSurface = textureList[randomTexturesYes]
			v.RightSurface = textureList[randomTexturesYes]
			v.FrontSurface = textureList[randomTexturesYes]
			v.BackSurface = textureList[randomTexturesYes]

		end
		if faces then
			local d = Instance.new("Decal", v)
			d.Texture = "rbxassetid://925874207"
		end

		v.Name = yesyes.." | #"..i

		v.Color = Color3.fromRGB(math.random(0,255), math.random(0,255), math.random(0,255))
	end
end
1 Like

The problem is that you are trying to get the length of a dictionary, not a regular list. I suggest changing it to one by simply doing this:

local randomEvents = {
	{
	"LowGravity" , 
	{Time = math.random(10,20),
	Sky = game.ReplicatedStorage.Events.LowGravity,
	Gravity = 20
	}},
	{
	"MarbleExplosion",
	{
	BlastRadius = 3000000,
	BlastPressure = 1000000
	}}
}

So you would need to do this

local randomEvent = math.random(1,#randomEvents)
local eventname = randomEvents[randomEvent][1]
local eventproperties = randomEvents[randomEvent][2]
2 Likes

As mechclash said you need an array-like table to get its count. Another option to changing randomEvents would be to create a simple array of keys just after you define randomEvents.

local randomKeys = {}
for key,_ in randomEvents do
  table.insert(randomKeys, key)
end

Now you can get a random element from the keys.

local randomEvent = randomKeys[math.random(1,#randomKeys)]

1 Like

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