local teleport = game.ReplicatedStorage:WaitForChild(“Teleportplayer”)
local teleportservice = game:GetService(“TeleportService”)
local placeId = 9875052903
local players = game:GetService(“Players”)
local player = players:GetPlayers()
teleport.OnServerEvent:Connect(function()
teleportservice:TeleportAsync(placeId,{player})
end)
If you just want to teleport you then
local teleport = game.ReplicatedStorage:WaitForChild(“Teleportplayer”)
local teleportservice = game:GetService(“TeleportService”)
local placeId = 9875052903
teleport.OnServerEvent:Connect(function(Player)
teleportservice:TeleportAsync(placeId,Player)
end)
Hey there,
I recommend on using RemoteEvent for this solution. We have the client fire a remote event onto the server, then the server will teleport the player that fired the event.
Of which you seems to already be using, but you’re doing it incorrectly.
In case I misunderstood, the title of this post says: “I cant get the button to take me to the game”, So
I’m going to assume that you’re trying to teleport the player that presses a button, so we will do that.
Step By Step
Now, here’s a step-by-step tutorial which should help you understand what we’re really doing here instead of just copy and pasting the code directly, which is already provided below within the Final Code section. So skip this if you’re lazy or just don’t care.
1. Remote Creation
Firstly, let’s make a remote event, the best place is in the ReplicatedStorage, as it allows for both client and the server to access said remote!
Now, name the event to whatever you want, just make sure that when defining the variable within the script, it is correct and not inputted wrongly. Now, for the sake of this post, I’m going to name it, “TeleportPlayer” event.
If you’ve done this correctly, the remote should look like this:
Now, let’s move onto the next step!
2. The Interface
Alrighty, next up we need the client in order to fire the event. But, we need the button first before we go over the client-stuff. Head to StarterGui and find your proposed ScreenGui with a button that you want the player to press. Now, this works with both TextButton and ImageButton as its event inherits from GuiButton.
For me, I’ll just make a simple UI with a text button for the sake of this tutorial.
And alas, it should look like this:
Now, you may move the button to your desired position on the screen.
It’s really up to you since it’s user preferential.
3. The Client
We’re here, now let’s get to it. Start off by inserting a LocalScript into the ScreenGui that we’ve just made in step 2. It should look like this after you’ve inserted the said object:
Now, double-press the script to open the Script Editor. It should have the template code of which it prints “Hello world!” for when you play the game. What you’re going to do next is remove that line of code and you should have nothing. If you’ve done that, let’s get to it.
Services
Start off by defining the services that we’re going to be using. For this, we’re going to use the GetService() function derived from the game itself. You could do it manually, but I prefer the function more:
-- Option 1
local ReplicatedStorage = game.ReplicatedStorage
--[[ Don't recommend as you can rename the service in the explorer
Therefore, will result in an error.
]]
-- Option 2
local ReplicatedStorage = game:GetService("ReplicatedStorage")
Button
Now, let’s define the button. Be careful on this step as it might not work for you. Remember what I said in step 1, “when defining the variable within the script, it is correct and not inputted wrongly”? Yeah, it’s pretty much that. Take a look at this picture and the code here for comparison:
local Button = script.Parent.TextButton
So what this means is that we’re getting the script’s parent, which is the ScreenGui which we then top it off with TextButton which is also a child of ScreenGui. (I don’t know how to explain it, but I hope you understand the basics of it).
Event
Same thing as the Button, now we must define the remote event which we then will use to fire onto the server. Look at where your event is located, here’s mine:
Oh, look! It’s in ReplicatedStorage. We already got the service before, so I’m going to name the variable something simple so that I can remember and the code should look like this:
local TeleportPlayerEvent = ReplicatedStorage.TeleportPlayer
Function and Connection
I’m sure you already know the purpose of functions and what it does and do, but if you still yet don’t know- here’s a page for it: Functions
Now, let’s name the function and fire the event to the server with the FireServer() method:
local function onMouseClick()
TeleportPlayerEvent:FireServer()
end
This function will be connected to when the TextButton’s MouseButton1Click event is activated. Now, simply call the click method and connect it with the local function that we’ve just made earlier:
Button.MouseButton1Click:Connect(onMouseClick)
And there you go! Next time the client clicks the button, the event should now fire. But where does it go? Nowhere because the server isn’t picking it up yet, so let’s move on to the final step!
Next up, open up the script and in to the Script Editor we go. It should have the print code again, just remove it as it is not what we’re trying to do here.
local TeleportService = game:GetService("TeleportService")
We will be using this service to teleport the player.
Now, once again, define the service and the remote like you did before for the client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportPlayerEvent = ReplicatedStorage.TeleportPlayer
Oh, and don’t forget to make a variable for the PlaceId, just insert the id of the place/game for the player to be teleported to:
local PlaceId = 41324860
Next, we’re going to make a function for when the event is picked up from the client by the server and also use TeleportService’s method, TeleportAsync. Make sure you put in the parameters correctly and it should look like this:
local function onTeleportEvent(player: Player)
TeleportService:TeleportAsync(PlaceId, {player})
end
We’re only teleporting the player, nothing more than that. The player argument is the client that fired the event in the first place which is the Player object, just to clarify- hehe.
Lastly, we’re going to connect the event to the function we just made so that it picks up whenever FireServer() is fired by the client:
Now, congratulations. Plug in, and play. Note that teleporting does not work in Studio and that it only works in live-game, this is not false as it has been proven before so if it errors, it isn’t you or the code’s fault:
For the sake of OP’s requests, I have made it so that the code below is compatible with theirs.
Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Button = -- Path to your button
local TeleportPlayerEvent = ReplicatedStorage.Teleportplayer
local function onMouseClick()
TeleportPlayerEvent:FireServer()
end
Button.MouseButton1Click:Connect(onMouseClick)
(Dear, OP, you need to define the button yourself, as I, don’t have access to your explorer)
Server
local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportPlayerEvent = ReplicatedStorage.Teleportplayer
local PlaceId = 9875052903
local function onTeleportEvent(player: Player)
TeleportService:TeleportAsync(PlaceId, {player})
end
TeleportPlayerEvent.OnServerEvent:Connect(onTeleportEvent)
And that pretty much covers up the entire thing. I hope you enjoyed, this took me a while to write all of this down but it sure was worth it. If there’s anything I said that is wrong or incorrect, please do correct me as I’m happy to learn new things, thank you!