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.

function Click()
script.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(-200, 8, 171.6) 
end

script.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:

local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Down:Connect(function()
    local char = plr.Character
    if char then
        char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(-200, 8, 171.6)
    end
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

local remote = Instance.new("RemoteEvent", game.ReplicatedStorage)
remote.Name = "Remote"

remote.OnServerEvent:Connect(function(plr, arg)
    if arg == "TP" then
        local char = plr.Character
        if char then
            char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(-200, 8, 171.6)
        end
    end
end)

LocalScript

local remote = game.ReplicatedStorage:WaitForChild("Remote")
script.Parent.MouseButton1Down:Connect(function()
    remote:FireServer("TP")
end)
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.

-- LocalScript

local plr = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportEvent = ReplicatedStorage:WaitForChild("TpEvent")
local char = plr.Character or plr.CharacterAdded:Wait() 
local Click

Click = function()
   TeleportEvent:FireServer()
end

script.Parent.MouseButton1Click:Connect(Click)
-- 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:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportEvent = ReplicatedStorage:WaitForChild("TpEvent")

TeleportEvent.OnServerEvent:Connect(function(player)
    player.Character.HumanoidRootPart.CFrame = CFrame.new(-200, 8, 171.6)
    -- Don't use torso, as R15 rigs don't have that (not that you were using R15) All rigs have a HumanoidRootPart
end)
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 .

function Click()
game.Players.LocalPlayer.Character.UpperTorso.CFrame = CFrame.new(-200, 8, 171.6) 
end

script.Parent.MouseButton1Down:connect(Click)

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

Answer this question