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
1 | function onClicked(playerWhoClicked) |
2 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
3 | if player then |
4 | game:GetService( "TeleportService" ):Teleport( 8473728 , player) |
5 | end |
6 | end |
Thanks -littelbigblox
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:
1 | script.Parent.Adornee = workspace.Part |
2 |
3 | function onClicked() |
4 | if game.Players.LocalPlayer then |
5 | game:GetService( "TeleportService" ):Teleport( 8473728 ) -- The player to Teleport is assumed when called from a LocalScript. |
6 | end |
7 | end |
8 |
9 | script.Parent.Frame.TextButton.MouseButton 1 Click:connect(onClicked) |
1 | function onClicked(playerWhoClicked) |
2 | local player = game.Players [ playerWhoClicked ] |
3 | if player then |
4 | game:GetService( "TeleportService" ):Teleport( 8473728 , player) |
5 | end |
6 | end |