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

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

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

✓ Accepted 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.

2 comments

0

Should I use a server script or local script?

0

Nevermind, it worked.

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)

2 comments

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,

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

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