Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I use TeleportService in my code?

Asked by 4 years ago

Hello! I'm working on a game where when you touch an object you are teleported to a private server in a different place. I have recently discovered TeleportService and read about it on the Roblox developer site, but I still can't understand what some of it does. First of all, the explanation for "ReserveServer" was pretty vague, so I don't understand what that affects in the code. Also, does the place that is trying to be teleported to have to be in the same game? Here is my code:

function onTouched(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
    if script.Parent.Active.Value == true then
        local TS = game:GetService("TeleportService")
        local code = TS:ReserveServer(game.PlaceId) -- Returns a code
        local Players = game:GetService("Players")
        local players = Players:GetPlayers() -- Get a list of all players   

        TS:TeleportToPrivateServer(script.Parent.PlaceID.Value, code, players)
        end
    end
end

script.Parent.Touched:connect(onTouched)

Clarification: First the code did nothing, so I messed around with a few variables, that's why I'm using the "players" variable instead of "player".

Sorry if this is obvious, I am new to scripting.

0
Why do you have script.Parent.PlaceID.Value when you can just use game.PlaceId? firestarroblox123 440 — 4y
0
Wouldn't that teleport the player to the current place/rejoin? I'm looking to teleport the player to an entirely separate place. proqrammed 285 — 4y

Answer this question