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 6 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:

Nyes.MouseButton1Down:Connect(function()

end)

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 — 6y

2 answers

Log in to vote
0
Answered by 6 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

local player = game.Players.LocalPlayer
local button = script.Parent

button.MouseButton1Down:Connect(function()
    local char = player.Character -- Get the character
    if char then -- if the char isn't loaded it won't run the code, this is needed to prevent it from breaking
        local HRP = char:WaitForChild('HumanoidRootPart')
    end
end)

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

local player = game.Players.LocalPlayer
local button = script.Parent

local partTT = workspace:WaitForChild('TeleportPart') -- Set it to the part you want to teleport to

button.MouseButton1Down:Connect(function()
    local char = player.Character -- Get the character
    if char then -- if the char isn't loaded it won't run the code, this is needed to prevent it from breaking
        local HRP = char:WaitForChild('HumanoidRootPart')
        HRP.CFrame = partTT.CFrame
    end
end)

If you want to know the other way...

local player = game.Players.LocalPlayer
local button = script.Parent

local pos = CFrame.new(Vector3.new(0,0,0)) -- You can change the pos
button.MouseButton1Down:Connect(function()
    local char = player.Character -- Get the character
    if char then -- if the char isn't loaded it won't run the code, this is needed to prevent it from breaking
        local HRP = char:WaitForChild('HumanoidRootPart')
        HRP.CFrame = pos
    end
end)

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

1
nice awesomeipod 607 — 6y
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 — 6y
Ad
Log in to vote
0
Answered by 6 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