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?
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)
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.
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.