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

Why won't my teleport button script work?

Asked by 8 years ago
function onClicked () 
    local player = game.Players.LocalPlayer
        if player then
        game:GetService("TeleportService"):Teleport(283976037, player)
    end
end
game.StarterGui.ScreenGui.TextButton.MouseButton1Down:connect(onClicked)

Honestly this is probably "my" first "complicated" script.....

1 answer

Log in to vote
4
Answered by 8 years ago

Well first, you have the arguments inverted for the "Teleport" function. The "player" variable should be first, and the place id second. Like this:

function onClicked () 
    local player = game.Players.LocalPlayer
    if player then
        game:GetService("TeleportService"):Teleport(player, 283976037)
    end
end
game.StarterGui.ScreenGui.TextButton.MouseButton1Down:connect(onClicked)

And as TheAlphaStigma pointed out, you're trying to set an event to something in the StarterGui service, which does not automatically update to individual players. You should use the ancestry chain of the script, by using the "Parent" property.

For example:

-- Also make sure this is a local script, since we're getting the LocalPlayer from the Players service.

function onClicked () 
    local player = game.Players.LocalPlayer
    if player then
        game:GetService("TeleportService"):Teleport(player, 283976037)
    end
end

-- Assuming this script is inside the TextButton GUI, this would work.

script.Parent.MouseButton1Down:connect(onClicked)
0
Although that is one of the problems, take a look at the code again (hint: Line 7). :) TheeDeathCaster 2368 — 8y
0
Yes, there's also that. ScriptGuider 5640 — 8y
Ad

Answer this question