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.
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
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:
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
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
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.
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]