Roblox gives false error message

Hello, I’m currently making a teleport system. But roblox gives me the following message in the output:

ServerScriptService.TPScript:11: invalid argument #2 to ‘random’ (interval is empty)

I am doing

math.random(1, #children)

And children is defined as a folder in workspace containing 1 player, me. Yet it gives the error. And yes it’s a serverscript that puts me in the folder.

#children

Don’t mind the name :slight_smile:

1 Like

I think you’re trying to teleport a number instead of a player, if you’re using :GetChildren() to get all players which is what you’re doing then you would use this

children[math.random(1, #children)]

Either, your table is empty or you have named the items in your table.
Make sure you are getting children whenever you need to, and not just once.
Your table should look like this if you print it:

{
    [1] = Character;
}

I’m doing

local num = math.random(1, #children)
local player = children[num]

It’s not a table, it’s a folder in workspace that contains the player

Using GetChildren() returns a table, if youre not using GetChildren() thats entirely why youre getting the error.

I’ll send the whole script if needed:

local children = workspace.RedTeam:GetChildren()
local parts = workspace.RedParts:GetChildren()
local parts1 = game.Workspace.RedParts
local children1 = game.Workspace.RedTeam

wait(15)

local num = math.random(1, #parts)
local randomplace = parts[num]
print(#children)
local randomplayernum = math.random(1, #children)
local player1 = children[randomplayernum]

wait(3)

if player1 and randomplace then
	if #parts <= 0 then
		print("Parts Empty")
		return
	end
	if #children <= 0 then
		print("Children Empty")
		return
	end
	player1.PrimaryPart.CFrame = randomplace.CFrame
	
	player1.Parent = workspace.TPPlayersRed
	randomplace.Parent = workspace.TPPartsRed
	
	if player1 and randomplace then
		wait(2)
		children = game.Workspace.RedTeam:GetChildren()
		parts = game.Workspace.RedParts:GetChildren()
		if #parts <= 0 then
			print("Parts Empty")
			return
		end
		if #children <= 0 then
			print("Children Empty")
			return
		end
		num = math.random(1, #parts)
		randomplace = parts[num]
		
		randomplayernum = math.random(1, #children)
		player1 = children[randomplayernum]
		player1.PrimaryPart.CFrame = randomplace.CFrame
		player1.Parent = workspace.TPPlayersRed
		randomplace.Parent = workspace.TPPartsRed
		
		if player1 and randomplace then
			wait(2)
			
			children = game.Workspace.RedTeam:GetChildren()
			parts = game.Workspace.RedParts:GetChildren()
			if #parts <= 0 then
				print("Parts Empty")
				return
			end
			if #children <= 0 then
				print("Children Empty")
				return
			end
			num = math.random(1, #parts)
			randomplace = parts[num]

			randomplayernum = math.random(1, #children)
			player1 = children[randomplayernum]
			
			player1.PrimaryPart.CFrame = randomplace.CFrame
			
			randomplace.Parent = workspace.TPPartsRed
			player1.Parent = workspace.TPPlayersRed
		end
	end
end

Are you using :GetChildren() to get the characters in the folder in workspace?

you will also need to use :GetPlayerFromCharacter(yourchar) to actually teleport the player, as you can’t teleport using just the character instance

It has worked before with just :GetChildren().

I’m using char.PrimaryPart.CFrame = workspace.Part.CFrame

^^ is just an example

Try printing out the children variable when you update it, and the # of items in it.

print(children, #children)

Also, add some code to check if the number of children is greater than 0 before using math.random, to avoid errors.

if #children > 0 then
    --code here
else
    print('# of children = 0')
end

Yeah, I’ve printed out the value of children and it says 0, but when I check there is me in the folder. SO it should be 1. It’s not client sided either

Thats why im saying to also print out the table, to verify that you are in it, if not, youve probably written your code a little wrong.

You shouldn’t do this, this is giving the children at the time you called GetChildren, and doesn’t update. Instead, call :GetChildren() on them when you use them, AKA on the same line or without a wait in between. Here a simple fixed section of your script, to show you how you should be using :GetChildren:

local children = workspace.RedTeam
local parts = workspace.RedParts
local parts1 = game.Workspace.RedParts
local children1 = game.Workspace.RedTeam

wait(15)

local num = math.random(1, #parts:GetChildren())
local randomplace = parts[num]
print(#children:GetChildren())
local randomplayernum = math.random(1, #children:GetChildren())
local player1 = children[randomplayernum]

Nothing is in the table because the game has only just loaded, and the scripts that add objects to the folders/models haven’t run yet, which makes math.random throw an error.

Try this:

math.random(1,math.max(1,#children))

The error is caused by the table being empty, using math.max will help you avoid this error.

image
image
While there are 3 parts, it gives me this error ^^

That’s because it’s looking for parts with the random number as the name. You need to add another :GetChildren() before the [, like this:

local randomplace = parts:GetChildren()[num]

I’m not sure if this was intentional but it looks like you are using - instead of =.

Secondly, here’s a revised version of your script.

local num = math.random(1,math.max(1,#parts:GetChildren()))
local randomplace = parts:GetChildren()[num]
print(#children:GetChildren())
local randomplayernum = math.random(1,math.max(1,#children:GetChildren()))
local player1 = children:GetChildren()[randomplayernum]]

This happens because you’re telling the script to look for children named[number] instead of looking for the index {‘1’}[1]

Works, but it breaks when I do children[num]

Yeah, you are supposed to do children:GetChildren()[num].

Works, but I have another error.

player1.Parent = workspace.TPPlayersRed

Doesn’t do anything. Just keeps me in old folder.