What do you want to achieve?
I’d like to have the player teleported to a different game when they press the Textbutton within the ScreenGui
What is the issue?
It won’t work.
What solutions have you tried so far?
I’ve re-written the code multiple times but it still wont work.
Also, I have ‘Allow 3rd Party Teleports’ On in Game Settings.
local teleportButton
local TeleportService = game:GetService("TeleportService")
function onTeleportButtonClicked()
local placeId = 578872909
TeleportService:Teleport(placeId, game.Players.LocalPlayer)
end
-- This function is called when the button is clicked
teleportButton = script.Parent:FindFirstChild("TeleportButton")
if teleportButton then
teleportButton.MouseButton1Click:Connect(onTeleportButtonClicked)
end
In this line, you are just saying if LocalScript.Parent == TeleportButton.TeleportButton
You could just add another .Parent to the line or just keep it as script.Parent since you are going
through the Buttons Descendants instead.
local teleportButton
local TeleportService = game:GetService("TeleportService")
function onTeleportButtonClicked()
local placeId = 578872909 -- Replace with the actual place ID of the destination game
TeleportService:Teleport(placeId, game.Players.LocalPlayer)
end
-- This function is called when the button is clicked
-- Create the button and add it to the Surface GUI
teleportButton = script.Parent
if teleportButton then
teleportButton.MouseButton1Click:Connect(onTeleportButtonClicked)
end
Then in a server script, within ServerScriptService area,
game.ReplicatedStorage.RemoteNameHere.OnServerEvent:Connect(function()
-- the rest of your teleportation code here (add the local values too, very important)
end)
Just realised you are using a surfaceGui. LocalScripts don’t work in Workspace, I think. You would need to create a screenGui and insert the exact same gui and set the adornee property to the part you would like the telport button to be. Then, follow the steps from YourBossyLord.
steps:
Put your surfaceGui in your screengui, it can be in a folder called ‘surface gui’
Set adornee property to part face by clicking on it then the part you want it to be adorned on.
Thank you for creating those steps! That is EXTREMELY helpful!
For my ServerScript, is this what it should look like?
game.ReplicatedStorage.GameTeleport.OnServerEvent:Connect(function()
-- the rest of your teleportation code here (add the local values too, very important)
local teleportButton
local TeleportService = game:GetService("TeleportService")
function onTeleportButtonClicked()
local placeId = 578872909 -- Replace with the actual place ID of the destination game
TeleportService:Teleport(placeId, game.Players.LocalPlayer)
end
teleportButton = script.Parent
if teleportButton then
teleportButton.MouseButton1Click:Connect(onTeleportButtonClicked)
end
game.ReplicatedStorage.GameTeleport:FireServer()
end)
But `onTeleportButtonClicked()’ is underlined in orange
Then my local script looks like this:
local teleportButton
local TeleportService = game:GetService("TeleportService")
function onTeleportButtonClicked()
local placeId = 578872909 -- Replace with the actual place ID of the destination game
TeleportService:Teleport(placeId, game.Players.LocalPlayer)
end
-- This function is called when the button is clicked
-- Create the button and add it to the Surface GUI
teleportButton = script.Parent
if teleportButton then
teleportButton.MouseButton1Click:Connect(onTeleportButtonClicked)
end
game.ReplicatedStorage.GameTeleport:FireServer()
local teleportButton
local TeleportService = game:GetService("TeleportService")
function onTeleportButtonClicked()
local placeId = 578872909 -- Replace with the actual place ID of the destination game
TeleportService:Teleport(placeId, game.Players.LocalPlayer)
end
-- This function is called when the button is clicked
-- Create the button and add it to the Surface GUI
teleportButton = script.Parent
if teleportButton then
teleportButton.MouseButton1Click:Connect(onTeleportButtonClicked)
end
On your ServerScript, you have a line that fires the server, remove that. Also, you use game.Players.LocalPlayer to teleport the player in the ServerScript, but the server does not know who the local player is. You can fetch the plr value when the event is fired by doing this!
Local:
-- This function is called when the button is clicked
-- Create the button and add it to the Surface GUI
teleportButton = script.Parent
if teleportButton then
teleportButton.MouseButton1Click.FireServer:Connect()
game.ReplicatedStorage.GameTeleport:FireServer()
end)
end
Server:
game.ReplicatedStorage.GameTeleport.OnServerEvent:Connect(function(plr) -- see plr here, that is how you fetch it
-- the rest of your teleportation code here (add the local values too, very important)
local TeleportService = game:GetService("TeleportService")
local placeId = 578872909 -- Replace with the actual place ID of the destination game
TeleportService:Teleport(placeId, plr)
-- no need for function, can just do it
end)
EDIT: no need to fire server, i think @ProWJH said you can just teleport in local. Just use the same fixed local script with no fire server,