I have created a simple teleport gui in starter gui that will teleport a player to coordinates on click.
1 | function Click() |
2 | script.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(- 200 , 8 , 171.6 ) |
3 | end |
4 |
5 | script.Parent.MouseButton 1 Down:connect(Click) |
When I test it out in studio and click on the gui it works and teleports me. But when I join a server in my game it doesn't teleport. Any help?
If it's a LocalScript, use LocalPlayer
, like this:
1 | local plr = game.Players.LocalPlayer |
2 | script.Parent.MouseButton 1 Down:Connect( function () |
3 | local char = plr.Character |
4 | if char then |
5 | char:WaitForChild( "HumanoidRootPart" ).CFrame = CFrame.new(- 200 , 8 , 171.6 ) |
6 | end |
7 | end ) |
However if it's a server script, then either make it a LocalScript or make another LocalScript which will fire a RemoteEvent
telling the server script to teleport the player upon clicking the button, like this:
Server Script
01 | local remote = Instance.new( "RemoteEvent" , game.ReplicatedStorage) |
02 | remote.Name = "Remote" |
03 |
04 | remote.OnServerEvent:Connect( function (plr, arg) |
05 | if arg = = "TP" then |
06 | local char = plr.Character |
07 | if char then |
08 | char:WaitForChild( "HumanoidRootPart" ).CFrame = CFrame.new(- 200 , 8 , 171.6 ) |
09 | end |
10 | end |
11 | end ) |
LocalScript
1 | local remote = game.ReplicatedStorage:WaitForChild( "Remote" ) |
2 | script.Parent.MouseButton 1 Down:Connect( function () |
3 | remote:FireServer( "TP" ) |
4 | end ) |
RemoteEvent
to teleport. Also, if you were trying to access the Player
object using script.Parent.Parent.Parent.Parent
, don't. Using so many parents is ugly style and hard to understand. Since your script is a LocalScript
, you can use LocalPlayer
.01 | -- LocalScript |
02 |
03 | local plr = game:GetService( "Players" ).LocalPlayer |
04 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
05 | local TeleportEvent = ReplicatedStorage:WaitForChild( "TpEvent" ) |
06 | local char = plr.Character or plr.CharacterAdded:Wait() |
07 | local Click |
08 |
09 | Click = function () |
10 | TeleportEvent:FireServer() |
11 | end |
12 |
13 | script.Parent.MouseButton 1 Click:Connect(Click) |
14 | -- Connect, connect is deprecated. You may want to use Button1Click, as Button1Down code runs while the mouse is down on the gui. Button1Click runs code just on the click. |
1 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
2 | local TeleportEvent = ReplicatedStorage:WaitForChild( "TpEvent" ) |
3 |
4 | TeleportEvent.OnServerEvent:Connect( function (player) |
5 | player.Character.HumanoidRootPart.CFrame = CFrame.new(- 200 , 8 , 171.6 ) |
6 | -- Don't use torso, as R15 rigs don't have that (not that you were using R15) All rigs have a HumanoidRootPart |
7 | end ) |
I have had this error too. When speaking about GUI's and finding the player, don't throw in 6 script.Parent's and assume that it works. Because, it will only work on studio.
If your script is a LocalScript
then use game.Players.LocalPlayer
.
1 | function Click() |
2 | game.Players.LocalPlayer.Character.UpperTorso.CFrame = CFrame.new(- 200 , 8 , 171.6 ) |
3 | end |
4 |
5 | script.Parent.MouseButton 1 Down:connect(Click) |
If your game is FilteringEnabled(Most games) then add a RemoteEvent and fire it.