Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Surface GUI Teleport Problem?

Im trying to make a button so it teleports me to a game and its not working! My button is in the: Part>SurfaceGui>Frame>TextButton

function onClicked(playerWhoClicked)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        game:GetService("TeleportService"):Teleport(8473728, player)
    end
end

Thanks -littelbigblox

2 answers

SurfaceGuis (and also BillboardGuis) do not know the player that activated them unless the Gui itself is located within a specific Players' PlayerGui.

To start, move your SurfaceGui in its entirety to the StarterGui. Make this script a LocalScript.

Assuming 'Part' is in the Workspace, and this LocalScript is a direct descendant of the SurfaceGui:

script.Parent.Adornee = workspace.Part

function onClicked()
    if game.Players.LocalPlayer then
        game:GetService("TeleportService"):Teleport(8473728) -- The player to Teleport is assumed when called from a LocalScript.
    end
end

script.Parent.Frame.TextButton.MouseButton1Click:connect(onClicked)
function onClicked(playerWhoClicked)
    local player = game.Players[playerWhoClicked]
    if player then
        game:GetService("TeleportService"):Teleport(8473728, player)
    end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.