How can I teleport a player with a button?

Given the challenge of needing a teleporter, I decided to take a click detector and figure how I could make it work. So I used ROBLOX’s official tutorial (it did not help very much) CFrames | Documentation - Roblox Creator Hub
and I would like to know how the button can know who clicked it, and how to teleport them.

This is the script so far

local clickDetector = workspace.Button.ClickDetector

function onMouseClick()
game.Workspace.Player.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(0, 50, 0)) --insert script in here

end

clickDetector.MouseClick:connect(onMouseClick)
–crazygrapefruit

EDIT: button with a click detector inside my bad

1 Like

The first parameter of onMouseClick will be the player

Also if you’re teleporting a player to a certain XYZ place I rather use a part.Position than Vector3.new

I can’t fix the whole script for you because it wouldn’t help you learn

2 Likes

It’s something like this as Dolphin_Worm said earlier.

local clickDetector = script.Parent.ClickDetector

function onMouseClick(player)
	player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(0, 50, 0)) --insert script     in         here
end

clickDetector.MouseClick:connect(onMouseClick)
1 Like

That is for object click detectors
For Buttons
You can just get the Local Player and an Event that shows when they click it you may want to have some Debounce to prevent spamming and also Moving the Players’ Position can be done by this Manner

CHARACTER_OBJECT_HERE:MoveTo(POS_HERE, POS_OBJ_IF_POSSIBLE)

That’s because It’s an click detector object that can define who clicked it not the local player

Not useful tbh because he asks for a button click when the Player clicks it teleports them not a click detector

It’s a button to teleport a player not an Click Detector

2 Likes

Yes, sorry I meant button. so, like ColdCrazyGoku said, can I somehow take the function onMouseClick(player)
and take the “player” out and do something like player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(0, 50, 0))

and is (Vector3.new(0, 50, 0)) the position?

this is it

local position = Vector3.new(your position in xyz)
workspace.button.ClickDetector.MouseClick:Connect(function(player)
player.Character:MoveTo(position)
end)

this is all in a server script somewhere in workspace or server script service

1 Like

this is it :

 function onClicked(Player)
    	local torso = Player.Character:findFirstChild("HumanoidRootPart")
    	if torso ~= nil then
    		local playertest = Player.Character:findFirstChild("Humanoid")
    		if playertest ~= nil then
    			if playertest.Health > 0 then
    				--Set the position of the brick you want to teleport to into the "location" variable
    				local location = game.Workspace.floor1.Position
    				local x = location["X"]
    				local y = location["Y"]
    				local z = location["Z"]
    				
    				--Edit the numbers in the parentheses to affect the amount of randomness in the new position
    				x = x + math.random(-5, 5)
    				y = y + 3
    				z = z + math.random(-5, 5)
    				
    				local cf = torso.CFrame
    				torso.CFrame = CFrame.new(Vector3.new(x,y,z))
    			end
    		end
    	end
    end

    script.Parent.ClickDetector.MouseClick:connect(onClicked)
2 Likes