Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Teleport gui wont teleport in game?

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?

2 comments

0

Are you making this in R6 or R15

0

R6

3 answers

✓ Accepted answer

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)

13 comments

0

The parent argument to Instance.new is deprecated, do not feed OP deprecated code. Also, the TP argument is redundant.

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.

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.

0

It does matter. You're just saying it's deprecated without even knowing the reason why.

0

It was deprecated due to the latency.

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.

0

In general, it's deprecated. End of argument.

0

You just proved my point ^

0

How lol

0

The first example of the local script worked

0

@incapaz https://imgur.com/a/iolmssL as you can see, setting Parent manually afterwards decreases the performance even more.

###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)

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.