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

Why wont this Teleport players to the game?

Asked by 9 years ago

It is suppose to Teleport them to the place after clicking a GUI button

player = game:FindFirstChild("Player")

function onButtonClicked()

game:GetService("TeleportService"):Teleport(101314122, player)
end

script.Parent.MouseButton1Down:connect(onButtonClicked)

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

You need to define player by indexing the LocalPlayer of game.Players.


local player = game.Players.LocalPlayer

function onButtonClicked()
    game:GetService("TeleportService"):Teleport(101314122, player)
end

script.Parent.MouseButton1Down:connect(onButtonClicked)

NOTE: This should be used in a LocalScript.

Ad
Log in to vote
1
Answered by 9 years ago

Accessing the Player

There are many ways to access the player.

  • PlayerAdded(function)

  • LocalPlayer(property of player)

LocalPlayer, only works on a LocalScript, PlayerAdded on the other hand, only works on a ServerScript(Script). But right Now I'll be using LocalPlayer.

local player = game.Players.LocalPlayer

function onButtonClicked()
    game:GetService("TeleportService"):Teleport(101314122, player)
end

script.Parent.MouseButton1Down:connect(onButtonClicked)

Answer this question