Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to make a player teleport when the player has clicked a button?

Asked by 8 years ago

I need a script that I insert into a brick to make it so when a player clicks the brick, it teleports them to a location.

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago
local Part = script.Parent -- So you don't have to keep re-typing
local CD = Instance.new("ClickDetector",Part) --Creates a click detector inside of the part

CD.MouseClick:connect(function(player) -- When they click on the part
    if game.Workspace[player.Name]:FindFirstChild("Humanoid") then -- If it is a player
        game.Workspace[player.Name].Torso.CFrame = CFrame.new(0,3,0) -- Change this number
    end
end)

This is the simplest way I can think of to move a player's position. It probably isn't the most efficient or best way, but it works.

Ad
Log in to vote
0
Answered by
yut640 91
8 years ago
Edited 8 years ago

You need 2 blocks for this script.

Block one is the block you click

Block two is the block you teleport on top of (this makes it so you don't have to enter cframe values which I think is easier.)

Add a click detector to the block you click and a number value named enabled to both blocks make the value 1 for both. Then put this script in the brick:

------------------------------------
modelname="teleportlocation" -- rename this to a block that you want the player to teleport to
------------------------------------

function onClicked(part)
    if part.Parent ~= nil then
    local h = game.Players.LocalPlayer.Character.Humanoid
        if h~=nil then
            local teleportfrom=script.Parent.Enabled.Value
            if teleportfrom~=0 then
                if h==humanoid then
                return
                end
                local teleportto=script.Parent.Parent:findFirstChild(modelname)
                if teleportto~=nil then
                    local torso = h.Parent.Torso
                    local location = {teleportto.Position}
                    local i = 1

                    local x = location[i].x
                    local y = location[i].y
                    local z = location[i].z

                    x = x + math.random(-1, 1)
                    z = z + math.random(-1, 1)
                    y = y + math.random(2, 3)

                    local cf = torso.CFrame
                    local lx = 0
                    local ly = y
                    local lz = 0

                    script.Parent.Enabled.Value=0
                    teleportto.Enabled.Value=0
                    torso.CFrame = CFrame.new(Vector3.new(x,y,z), Vector3.new(lx,ly,lz))
                    wait(3)
                    script.Parent.Enabled.Value=1
                    teleportto.Enabled.Value=1
                else
                    print("Could not find teleporter!")
                end
            end
        end
    end
end

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

Tell me if you have any problems, I just tested and it is working.

Answer this question