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

How do I make a script that teleports the player that touches a part to another part?

Asked by 4 years ago

So, I do not know if I should be using a server script or local script. I have script, it does not work.
I want the player to teleport to part2 if they touch part1. The player should not be able to touch part1 to get to part2. Here is my script:

if script.Parent.IsTouched == true then
    game.Workspace.Player.Position = game.Workspace.Part2.Position
end

2 answers

Log in to vote
0
Answered by 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
function onTouched(hit)
    local human = hit.Parent:FindFirstChild("HumanoidRootPart")
    if hit then
        human.CFrame = game.Workspace.Part2.CFrame
    end
end

script.Parent.Touched:connect(onTouched)

This should be in the script of the Part that you want to be touched. Part2 should be renamed to the name of the part you want teleported to.

0
Should I use a server script or local script? GamingWithFlight 80 — 4y
0
Nevermind, it worked. GamingWithFlight 80 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Try this:

script.Parent.Touched:Connect(function(part)
    local char = part:FindFirstAncestorWhichIsA("Model")
    local dest_part = workspace:FindFirstChild("Part2")
    if char and dest_part then
        local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
        local dest = dest_part.CFrame
        if plr and dest then
            dest = dest + CFrame.new(0, 3.5, 0)
            if dest then
                char:SetPrimaryPartCFrame(dest)
            end
        end
    end
end)
0
You can't add a CFrame to a CFrame, and also the "part" is probarly a bodypart of the character, and the character is the parent of that bodyparts not a child of it, starmaq 1290 — 4y
0
And also always provide an explanation, not just giving the code without totally explaining anything, explain the functions you used like `:GetPlayerFromCharacter()`, and what you did in the script starmaq 1290 — 4y

Answer this question