I have a button that teleports a player if they are in a frame in StarterGui, but I keep getting a error called cant teleport object. The error is from this line tpservice:TeleportToPrivateServer(placeid, server, playertoteleport). I cant seem how to fix this, thanks for the help!
ServerScript:
game.ReplicatedStorage.Remotes.TeleportParty.OnServerEvent:Connect(function(plr)
local playergui = plr.PlayerGui
local server = tpservice:ReserveServer(placeid)
for i,v in pairs(playergui.Party.PlayersInParty.PlayerList:GetChildren()) do
if v then
if v:IsA('Frame') then
local playertoteleport = game.Players:FindFirstChild(v.PlayerName.Text)
print(playertoteleport.UserId)
tpservice:TeleportToPrivateServer(placeid, server, playertoteleport)
print('teleport for '.. playertoteleport.Name)
end
end
end
end)```
I would recommend just sending the players’ names through arguments. If this is server side, then when you look at what the playerGui is, it’s probably coming back as nil, because that would be locally stored
And you are trying to teleport only the player object each iteration of the loop with local playertoteleport = game.Players:FindFirstChild(v.PlayerName.Text) tpservice:TeleportToPrivateServer(placeid, server, playertoteleport)
Right now Im working with Teleport Service, and Teleporting to reserved servers too, and someone told me that TeleportToPrivateServer() is kinda useless now, cause all teleport functions has been added into TeleportAsync() :
And maybe adding a SaveTeleport module with TeleportService.TeleportInitFailed, like the example in the documentation would help a lot while doing this
I also would recommend using TeleportAsync instead of TeleportToPrivateServer. If you’re trying to teleport the user to their own reserved server then something like this should work:
game.ReplicatedStorage.Remotes.TeleportParty.OnServerEvent:Connect(function(plr)
local playergui = plr.PlayerGui
local TeleportService = game:GetService("TeleportService")
local PlaceID = game.PlaceId
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ShouldReserveServer = true
for i,v in pairs(playergui.Party.PlayersInParty.PlayerList:GetChildren()) do
if v then
if v:IsA('Frame') then
local playertoteleport = game.Players:FindFirstChild(v.PlayerName.Text)
print(playertoteleport.UserId)
TeleportService:TeleportAsync(PlaceID, {playertoteleport}, teleportOptions)
print('teleport for '.. playertoteleport.Name)
end
end
end
end)
Yup, its exactly what I was saying, it needs an array and TeleportAsync() should be used instead of TeleportToPrivateServer(), and I don’t know if it’s a matter of taste… but, we’ve been talking about this on my post. I think its more efficient creating the reserved server code before teleporting the player, instead of using ShouldReserveServer = true into TeleportOptions
By just doing:
local code = TeleportService:ReserveServer(placeID)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ReservedServerAccessCode = code
TeleportService:TeleportAsync(PlaceID, {Player}, teleportOptions)
local TeleportService = game:GetService("TeleportService")
local PlaceID = game.PlaceId
game.ReplicatedStorage.Remotes.TeleportParty.OnServerEvent:Connect(function(plr)
local playergui = plr.PlayerGui
-- Save this server code in DataStore if you need it later
local serverCode = TeleportService:ReserveServer(PlaceID)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ReservedServerAccessCode = serverCode
-- Array of players that will be teleported
local PlayersToTP = {}
for i,v in pairs(playergui.Party.PlayersInParty.PlayerList:GetChildren()) do
if v then
if v:IsA('Frame') then
local playertoteleport = game.Players:FindFirstChild(v.PlayerName.Text)
-- If player is found, add it into an array
if playertoteleport then
table.insert(PlayersToTP, playertoteleport)
print(playertoteleport.UserId, "added into array")
end
end
end
end
-- Teleport entire array of players
warn("Teleporting array of players to reserved server", serverCode)
TeleportService:TeleportAsync(PlaceID, PlayersToTP, teleportOptions)
end)
I tested your script, and it says attempt to teleport to a restricited place, does the game have to be public or does Studio Access to Api Services have to be enabled?
I tried the script 30 mins ago, works fine for me.
I simulated your GUI, I just added a frame with a textlabel with my user name, and the script placed me inside the array on loop after pushing the button, and then teleported me to a reserved server with the correct code, works nicely
Any other info or details about your place?
I think that only works in Studio. Its giving your Studio rights to connect with Roblox APIs.
I guess that doesnt make any difference to real game, only Studio access
Does the game need to be public? I just testing it right now and it still says teleport failed to a restricted place, do both the starting place and the place I’m teleporting to need to be public?