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

How to teleport a player, when he click a part?

script.parent.GL.Touched:Connect(function(h)
    local hum = h.Parent:FindFirstChild("Humanoid")
    if hum ~= nil then
        h.parent.HumanoidRootPart.CFrame = CFrame.new(workspace["TelElTo"].Position)
    end
end)

script.parent.GR.Touched:Connect(function(h)
    local hum = h.Parent:FindFirstChild("Humanoid")
    if hum ~= nil then
        h.parent.HumanoidRootPart.CFrame = CFrame.new(workspace["TelElTo"].Position)
    end
end)

What i have to change in the script, when a player CLICKED and not touched the part that he will be teleported?

2 answers

You would have to add a "ClickDetector" inside your part. Then you need to add a local script into PlayerStarter --> StarterPlayerScripts.

Here is an example:

My part inside the Workspace

Workspace -- > gameT (part) --> ClickDetector

My local script:

function clicked()
	print("Hello!")
end

game.Workspace.gameT.ClickDetector.MouseClick:Connect(clicked)

You'd have to use a ClickDetector instance. ClickDetector are used to allow Models/Parts to be clicked and fire an event upon being clicked (if the script is connected to it).

ServerScript

function OnMouseClick(Player) -- Keep in mind, player is always fired over as an argument
	print('The Player who clicked this is: '..Player.Name)
end

local Part = script.Parent

local ClickDetector = Instance.new('ClickDetector')
ClickDetector.Parent = Part

ClickDetector.MouseClick:Connect(OnMouseClick)

Put this code in a ServerScript and put the script inside a part you want to be clicked.

1 comment

0

You can learn more about ClickDetectors here: https://developer.roblox.com/en-us/api-reference/class/ClickDetector

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