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?
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)
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