[Solved] TeleportService and ReserveServer error

I need help, when I put the script I get an error:
image
and when I put the script in the console it goes perfectly

Script:

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
 
local code = TS:ReserveServer(4810749712) 
local players = Players:GetPlayers() 
wait(4)
TS:TeleportToPrivateServer(4810749712,code,players) 

please help me

Is that script on the client? (30 character

1 Like

The context of your script is vague. If this runs immediately when the server starts, it may see no players added and thus return an empty table, causing that error.

I also put in a 5 second wait and it doesn’t work

Try this: (not sure if it works)

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")

local code = game:GetService("TeleportService"):ReserveServer(4810749712)
local players = Players:GetPlayers() 
wait(4)
TS:TeleportToPrivateServer(4810749712,code,{players})

it does not work :confused:
image

the players variable is already an array of objects so that should not work. Can you run this and tell me what it prints?

for i, v in ipairs(players) do
print(v.Name)
end

EDIT: OP, run that code

Try this:

-- just make sure that there are players in the game before calling TeleportPlayers.
local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")

local function TeleportPlayers()
	local code = TS:ReserveServer(4810749712)
	local players = Players:GetPlayers() do
		TS:TeleportToPrivateServer(4810749712,code,players)
	end
end

wait(10)
TeleportPlayers()
1 Like

TeleportService does not work in Studio! make sure that you are testing in-game.

It gives an error indicating this, so something else is wrong with the code as it says the argument is nil. If it was not nil, it would say that it does not work in studio.

This error is strange because GetPlayers will (or should) return an array when it’s called regardless of if it’s blank or not. Please try debugging your code by adding print statements to verify that the items you’re working with are non-nil.

Thanks!!! it worked :smiley: , I don’t know what was wrong

2 Likes