Player Teleportation Error

Hello!

Recently, I started a game with a round system. I’m sure most of you know the drill: an intermission, lobby, teleporting players, etc.

However, my method for teleporting players to a random spawn on a map (using math.random) results in this error in the output:

ServerScriptService.MainServerScript:57: invalid argument #2 to 'random' (interval is empty)

Basically, I created a function to teleport players to a random spawn on a map, then called it later in the script:

local function teleportPlayers(players,spawns) -- creating the function
	for _, player in pairs(players) do
		if player.Character then
			if player.Character:FindFirstChild("HumanoidRootPart") then
				player.Character.HumanoidRootPart.CFrame = spawns[math.random(1,#spawns)].CFrame + Vector3.new(0,5,0)
			end
		end
	end
end

local map = workspace.MapA
local totalPlayers = game.Players:GetPlayers()

-- calling the function
teleportPlayers(totalPlayers,map.Spawns:GetChildren()) -- 'Spawns' is a folder with all of the spawns located inside of the map model

I’m sort of confused because I’ve created other round systems without any errors. There’s a math.random documentation on the Developer Hub, but I don’t understand where I’m going wrong here (as the spawns and map do exist; keep in mind I’m not too experienced with Roblox programming just yet).

If you need more information regarding the issue, feel free to ask. Thanks!

I would double-check that your spawns are anchored. If they’re non-collidable and are not anchored, they’ll fall through the map and therefor get deleted automatically, so your :GetChildren() call will return an empty table.

1 Like

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