Help with Error

Hello, I’m having a problem with a script line that breaks the game.

I keep getting an error everytime the game ends and then the game won’t loop, here’s the error I keep getting:

16:55:49.184 ServerScriptService.ServerScripts.MainScript:461: invalid argument #2 to ‘random’ (interval is empty) - Server - MainScript:461

I don’t know what to do and I’ve tried looking for solutions but they won’t work either, here is the line of code:

	local lobbyspawns = {}
	for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do
		if v and v.Name == "Spawn" then
			table.insert(lobbyspawns, v)
		end
	end
	for _, player in pairs(activecontestants) do
		if player then
			awardpoints(player, 1)
			if player.Character then
				local humanoid = player.Character:FindFirstChild("Humanoid")
				if humanoid then
					humanoid:UnequipTools()
				end
			end

			local randomspawn = lobbyspawns[math.random(1, #lobbyspawns)]
			player.Character:MoveTo(randomspawn.Position)

			local backpack = player:FindFirstChild("Backpack")
			if backpack then
				backpack:ClearAllChildren()
			end
		end
	end

If anyone knows how to solve this issue, please send any feedback, thank you.

Hmm, the error message is indicating that the random function doesn’t have any possible values to choose from, meaning that your lobbyspawns table must be empty at the point where it tries to pick a random value.

Check the first part of your script where you populate lobbyspawns table. Make sure that there are indeed “Spawn” objects in the “Lobby” in the game workspace. Sometimes, a capitalization or spelling mistake could be messing up the proper execution.

Double-checking and debugging them will likely resolve your issue!

==== EDIT ====
I will try to be more clear. Check if:

  1. Lobby object exists within Workspace.
  2. Lobby object has at least one child named Spawn.
  3. Spawn objects are not direct children of the Lobby object but are instead nested deeper in another structure, GetChildren will not succeed in finding them.

Add print statements to debug:

local lobbyspawns = {}
for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do
    if v and v.Name == "Spawn" then
        table.insert(lobbyspawns, v)
    end
end

print("#lobbyspawns: "..#lobbyspawns) -- Add to debug

for _, player in pairs(activecontestants) do
    if player then
        awardpoints(player, 1)
        if player.Character then
            local humanoid = player.Character:FindFirstChild("Humanoid")
            if humanoid then
                humanoid:UnequipTools()
            end
        end

        if #lobbyspawns > 0 then  -- Add this check
            local randomspawn = lobbyspawns[math.random(1, #lobbyspawns)]
            player.Character:MoveTo(randomspawn.Position)
        else
            print("No spawn points available") -- Add to debug
        end
        
        local backpack = player:FindFirstChild("Backpack")
        if backpack then
            backpack:ClearAllChildren()
        end
    end
end

If the debug printout of #lobbyspawns is 0, then you don’t have any ‘Spawn’ in your game Workspace “Lobby”. It’s either inspecting the wrong place or “Spawn” objects don’t exist. If this check passes but the game can’t find spawn points later, then something is dynamically removing these spawn points.

I fixed the problem, thank you for the help!

1 Like

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