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