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

Problem with an error: invalid argument #2 to 'random' (range is empty) ?

So I'm trying to implement a script where players in the 'prisoners' team are teleported to a unique cell that will be given only to them. But with the script below I get an error:

ServerScriptService.Script:22: invalid argument #2 to 'random' (interval is empty)

Voici mon script :

local RemoteEvent = game.ReplicatedStorage.ChangeTeam

FreeCellsList = {
	CL1 = workspace.CellLocation1,
	CL2 = workspace.CellLocation2,
	CL3 = workspace.CellLocation3
}

RemoteEvent.OnServerEvent:Connect(function(player,teamcolor)
	player.TeamColor = teamcolor
	player:LoadCharacter()
	wait(0.1)
	
	--Underneath, assign a cell for each player in the team 
	
	local keys = {}
	for k in pairs(FreeCellsList) do 
		table.insert(keys,k)
	end
	
	if player.TeamColor == BrickColor.new("Deep orange") then
		local ChoiceIndex = math.random(1,#FreeCellsList)
		local ChoiceKey = keys[ChoiceIndex]
		local choice = FreeCellsList[ChoiceKey]
		table.remove(FreeCellsList,ChoiceIndex)
		player.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.choice.Position)
		print("Téléporté à une cellule aléatoire")
	end
end)

Thanks in advance !

2 comments

0

Why is the "teleport print" in spanish or something?

0

Its french cause Im french lol

1 answer

✓ Accepted answer

On line 22, the FreeCellsList table might be empty, which in turn causes #FreeCellsList to be 0. Putting 0 as the max number for the RNG to choose causes problems.

Also, for some reason getting the length of tables doesn't work when they are dictionaries (or tables that use keys to index values [e.g ["foo"] = 20]). Arrays however will work (tables that have uses numbers to index values.). This might've been the main problem you're dealing with.

Dictionary vs Array:

local dictionary = { -- [key] = <some value>
	["foo"] = 20,
	["bar"] = 124,
	
}
local array = { -- <value>,<value>,<value>
	"foo",
	"bar",
	true
{

You can stop the error by adding an if-statement, and get the actual count by looping through the dictionary.


local RemoteEvent = game.ReplicatedStorage.ChangeTeam

FreeCellsList = {
    CL1 = workspace.CellLocation1,
    CL2 = workspace.CellLocation2,
    CL3 = workspace.CellLocation3
}

local function CountTable(t) -- counts the number of items in a dictionary.
	local Total = 0
	if typeof(t) == "table" then
		for _, _ in pairs(t) do -- the key and value in the dictionary isn't needed.
			Total += 1
		end
	end
	return Total
end

RemoteEvent.OnServerEvent:Connect(function(player,teamcolor)
    player.TeamColor = teamcolor
    player:LoadCharacter()
    wait(0.1)

    --Underneath, assign a cell for each player in the team 

    local keys = {}
    for k in pairs(FreeCellsList) do 
        table.insert(keys,k)
    end

    if player.TeamColor == BrickColor.new("Deep orange") then
	local TableCount = CountTable(FreeCellsList) 
	if TableCount <= 0 then
		return -- or do something else.
	end
        local ChoiceIndex = math.random(1,TableCount)
        local ChoiceKey = keys[ChoiceIndex]
        local choice = FreeCellsList[ChoiceKey]
        table.remove(FreeCellsList,ChoiceIndex)
        player.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.choice.Position)
        print("Téléporté à une cellule aléatoire")
    end
end)

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