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

How do I tell this button who to teleport?

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 comment

1

.

2 answers

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

2 comments

1

nice

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.

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.

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