Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

"invalid argument #2 to 'random' (interval is empty)" problem?

Whenever I test-play my Hide and Seek game, I always see this error message appear: "invalid argument #2 to 'random' (interval is empty)". How do I solve this issue?

Full Code:

-- Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local status = replicatedStorage:WaitForChild("Values").Status

-- Config
local playersToStart = 1

-- Functions

local function chooseSeeker(availablePlayers)
	return availablePlayers[math.random(1, #availablePlayers)]
end

local function teleportPlayers(availablePlayers, spawns) -- availablePlayers: Table / spawns: Table
	for _, plr in pairs(availablePlayers) do
		if plr.Character then
			if plr.Character:FindFirstChild("HumanoidRootPart") then
				plr.Character.HumanoidRootPart.CFrame = spawns[math.random(1, #spawns)].CFrame + Vector3.new(0,5,6) <-- line with error message
			end
		end
	end
end

while wait(1) do
	
	repeat
		
		print("Not enough players in-game!")
		status.Value = playersToStart.." players or more are needed to start. (/"..playersToStart..")"
		
		wait(1)
		
	until #game.Players:GetPlayers() >= playersToStart
	
	for i = 5,0,-1 do
		status.Value = "Next round starts in "..i.." seconds."
		wait(0.1)
	end
	
	local contestants = game.Players:GetPlayers()
	
	local seeker = chooseSeeker(contestants)
	
	status.Value = "The chosen seeker is... "
	wait(2)
	status.Value = seeker.Name.."!"
	
	wait(3)
	
	local map = workspace.Map
	
	teleportPlayers(contestants, map.Spawns:GetChildren())
	wait(5)
end

6 comments

0

I'm not sure but I don't THINK # can be used in Lua coding

0

can be used in Lua.

0

Oh i didn't know that thanks

0

Quick Question what line does it say it has the error on the output on.

0

The line it says it has the error on the output on is Line 20.

0

Can't test right now so I don't know if this helps or not, you may already know this but pretty sure that the game is detecting that you have no spawn locations. Maybe test with printing spawns to see what it says.

1 answer

According to internet research, you would get an "interval is empty" error if the second argument is less than the first argument. Your spawns table may be empty at times, causing this error; check if this is the case.

This discussion is preserved as it was. New answers, comments, and votes are closed.