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

How do I tell this button who to teleport?

Asked by 7 years ago

I have a script which controls an entire GUI (it connects to all the parts of it etc) and one of the buttons needs to teleport the player who clicks it. Here's what that button's action looks like in the script so far:

1Nyes.MouseButton1Down:Connect(function()
2 
3end)

I know how to teleport a specified player, but I can't seem to make this teleport the player who clicks the button. Anyone know how I'd do that? Help appreciated

1
. awesomeipod 607 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

I see you don't understant client and server.

You don't have to tell the client who to teleport because each player has the same script and MouseButton1Down would run if that player clicks it.

Still don't understand?

You can get the local player by doing this local player = game.Players.LocalPlayer this doesn't work for a serverscript as there is only one server, there are multiple players.

To teleport that player just set his HRP Cframe (HRP = HumanoidRootPart)

You also didn't tell the script what 'Nyes' is so you'll either need script.Parent or tell it in a variable

Like this

1local player = game.Players.LocalPlayer
2local button = script.Parent
3 
4button.MouseButton1Down:Connect(function()
5    local char = player.Character -- Get the character
6    if char then -- if the char isn't loaded it won't run the code, this is needed to prevent it from breaking
7        local HRP = char:WaitForChild('HumanoidRootPart')
8    end
9end)

Then you'll have to set the CFrame, you have 2 options

1) Have a part already so you can teleport it to that part

2) Have the Coordinates so you can set it in the script

I'll use the first option

01local player = game.Players.LocalPlayer
02local button = script.Parent
03 
04local partTT = workspace:WaitForChild('TeleportPart') -- Set it to the part you want to teleport to
05 
06button.MouseButton1Down:Connect(function()
07    local char = player.Character -- Get the character
08    if char then -- if the char isn't loaded it won't run the code, this is needed to prevent it from breaking
09        local HRP = char:WaitForChild('HumanoidRootPart')
10        HRP.CFrame = partTT.CFrame
11    end
12end)

If you want to know the other way...

01local player = game.Players.LocalPlayer
02local button = script.Parent
03 
04local pos = CFrame.new(Vector3.new(0,0,0)) -- You can change the pos
05button.MouseButton1Down:Connect(function()
06    local char = player.Character -- Get the character
07    if char then -- if the char isn't loaded it won't run the code, this is needed to prevent it from breaking
08        local HRP = char:WaitForChild('HumanoidRootPart')
09        HRP.CFrame = pos
10    end
11end)

If you have any more questions, just ask, please set this as best answer :)

1
nice awesomeipod 607 — 7y
0
Thanks for the help. It's teleporting the person who clicks to another place in the game. Will that script work for this method? Also, I have already told the script what Nyes is, just I didn't see that as relevant to this question; hence I did not point it out. Thanks again. I'll see if this works tonight. DANISSY1 2 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Uhhhh, I am pretty sure that you should not have to tell the client who to teleport because each player has the same script and MouseButton1Down would run if that player clicks it.

Answer this question