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

Teleport gui wont teleport in game?

Asked by 6 years ago

I have created a simple teleport gui in starter gui that will teleport a player to coordinates on click.

1function Click()
2script.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(-200, 8, 171.6)
3end
4 
5script.Parent.MouseButton1Down: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?

0
Are you making this in R6 or R15 Deddlox 0 — 6y
0
R6 cooldrewbie 94 — 6y

3 answers

Log in to vote
1
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

If it's a LocalScript, use LocalPlayer, like this:

1local plr = game.Players.LocalPlayer
2script.Parent.MouseButton1Down:Connect(function()
3    local char = plr.Character
4    if char then
5        char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(-200, 8, 171.6)
6    end
7end)

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

01local remote = Instance.new("RemoteEvent", game.ReplicatedStorage)
02remote.Name = "Remote"
03 
04remote.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
11end)

LocalScript

1local remote = game.ReplicatedStorage:WaitForChild("Remote")
2script.Parent.MouseButton1Down:Connect(function()
3    remote:FireServer("TP")
4end)
0
The parent argument to Instance.new is deprecated, do not feed OP deprecated code. Also, the TP argument is redundant. User#19524 175 — 6y
0
It's perfectly fine to use the second argument of it when you are not creating any objects which are affected by physics nor changing their properties afterwards. Also, the TP argument is there because making one remote just for teleporting to a specific place is pointless. Amiaa16 3227 — 6y
0
It doesn't matter if the object is affected by physics. In general, the parent argument is D E P R E C A T E D. User#19524 175 — 6y
0
It does matter. You're just saying it's deprecated without even knowing the reason why. Amiaa16 3227 — 6y
View all comments (9 more)
0
It was deprecated due to the latency. User#19524 175 — 6y
0
The latency only occurs if you're creating a physical object such as part and then setting its properties afterwards, because roblox starts calculating the physics for it the moment it's parented. RemoteEvent is NOT a physical object and has no physics needed to be calculated. Amiaa16 3227 — 6y
0
In general, it's deprecated. End of argument. User#19524 175 — 6y
0
You just proved my point ^ Amiaa16 3227 — 6y
0
How lol User#19524 175 — 6y
0
The first example of the local script worked cooldrewbie 94 — 6y
0
@incapaz https://imgur.com/a/iolmssL as you can see, setting Parent manually afterwards decreases the performance even more. Amiaa16 3227 — 6y
Ad
Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

Scripts work in studio because everything runs locally. The client and server are not separated. They are one in Play Solo, which was the mode you used. You'll need a 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 
03local plr = game:GetService("Players").LocalPlayer
04local ReplicatedStorage = game:GetService("ReplicatedStorage")
05local TeleportEvent = ReplicatedStorage:WaitForChild("TpEvent")
06local char = plr.Character or plr.CharacterAdded:Wait()
07local Click
08 
09Click = function()
10   TeleportEvent:FireServer()
11end
12 
13script.Parent.MouseButton1Click: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.

Server code:

1local ReplicatedStorage = game:GetService("ReplicatedStorage")
2local TeleportEvent = ReplicatedStorage:WaitForChild("TpEvent")
3 
4TeleportEvent.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
7end)
Log in to vote
1
Answered by 6 years ago

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 .

1function Click()
2game.Players.LocalPlayer.Character.UpperTorso.CFrame = CFrame.new(-200, 8, 171.6)
3end
4 
5script.Parent.MouseButton1Down:connect(Click)

If your game is FilteringEnabled(Most games) then add a RemoteEvent and fire it.

Answer this question