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

Can somebody help me fix a broken teleport GUI button in my game?

Asked by 8 years ago

I'm making a GUI Button that teleports you to a place in my game, but it won't teleport!

 parent.MouseButton1Click()
    local teleportService=game:GetService("TeleportService")
    game:GetService('TeleportService'):Teleport(211564635)
    end

Can somebody help me fix this?

2 answers

Log in to vote
1
Answered by
woodengop 1134 Moderation Voter
8 years ago

The 'Problem'

Your Problem is that you need to position your event properly, parent.MouseButton1Click()is not valid because you need to connect your event to a function or listener.


parent.MouseButton1Click:connect(function() -- there now we have a connected event.
    game:GetService('TeleportService'):Teleport(211564635)
end)
Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
8 years ago

First, you must declare the function to teleport the player.

function onClick() --Defining the function. You could call this "function sup()", "function imthebestscripterever()", or whatever. As long as it starts with "function".
    local teleportService=game:GetService("TeleportService")
    teleportService:Teleport(211564635) --You don't need to do game:GetService("TeleportService") again because you already made a variable for it.
end

--[[
    Note that functions do not run when they're declared,
    only when they're called. You could put "onClick()" down
    here to call the function right after declaring it, which
    would immediately teleport the player to wherever he's going to go.
--]]

Then you must connect this function to an event. An event lets you call a function (run it) whenever something specific happens. MouseButton1Click is an event of TextButtons (as you'll see near the bottom). So, we can connect our function to this event so the function runs when the event happens. Like this:

script.Parent.MouseButton1Click:connect(onClick)

Full script:

function hello() --Declaring a function
    local teleportService=game:GetService("TeleportService")
    teleportService:Teleport(211564635)
end

script.Parent.MouseButton1Click:connect(hello) --Connecting to an event
0
It still does not work. I tried in a Script, and it won't teleport me! Help? crealol2 0 — 8y
0
Make sure it's a LocalScript funyun 958 — 8y
0
It still doesn't work! I don't know why! crealol2 0 — 8y
0
It has to be a LocalScript in a TextButton, and that TextButton should probably be in a ScreenGui, and that ScreenGui has to be in StarterGui. It works fine for me, even though it won't let me teleport to your game because it's not my game. funyun 958 — 8y

Answer this question