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 5 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:

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

2 answers

Log in to vote
0
Answered by 5 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.
1function onTouched(hit)
2    local human = hit.Parent:FindFirstChild("HumanoidRootPart")
3    if hit then
4        human.CFrame = game.Workspace.Part2.CFrame
5    end
6end
7 
8script.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 — 5y
0
Nevermind, it worked. GamingWithFlight 80 — 5y
Ad
Log in to vote
1
Answered by 5 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:

01script.Parent.Touched:Connect(function(part)
02    local char = part:FindFirstAncestorWhichIsA("Model")
03    local dest_part = workspace:FindFirstChild("Part2")
04    if char and dest_part then
05        local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
06        local dest = dest_part.CFrame
07        if plr and dest then
08            dest = dest + CFrame.new(0, 3.5, 0)
09            if dest then
10                char:SetPrimaryPartCFrame(dest)
11            end
12        end
13    end
14end)
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 — 5y
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 — 5y

Answer this question