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

Why is the player not teleporting when I use CFrame?

Asked by
stix619 15
10 years ago

I am having problems with moving a player that touches a brick to somewhere else. I have tried putting 'FindFirstChild("Humanoid")' as a variable and as a part of the 'onTouched' function, but niether have worked.

This is the code I have currently.

The script is a child of Part 'Part3'.

bin = script.Parent
player = game.Players:FindFirstChild("Humanoid")

function onTouched(Part3)
    if player then
        player.Torso.CFrame = CFrame.new(Vector3.new(84, 160, 12))
    end
end

bin.Touched:connect(onTouched)

I am a newbie to scripting, so I appreciate if you would explain the changes you would make to this code. Thank you in advance.

2 answers

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 years ago
bin = script.Parent

function onTouched(Part) --What was up with the three there? The Part is the part of the body hitting the "bin".
    if Part.Parent:FindFirstChild("Torso") then --Your Character is hitting it, not your player.
        Part.Parent.Torso.CFrame = CFrame.new(Vector3.new(84, 160, 12))
    end
end

bin.Touched:connect(onTouched)
0
It works! Thank you! stix619 15 — 10y
0
No problem, glad I could help. M39a9am3R 3210 — 10y
Ad
Log in to vote
1
Answered by
Sublimus 992 Moderation Voter
10 years ago

Here, I'll explain it as I go along:

bin = script.Parent

function onTouched(hit) -- 'hit' is the name we assign the object that touches it
    if hit.Parent:findFirstChild("Humanoid") -- The hit should be a humans leg, so we see if the parent has a humanoid in it as it should be a player
        hit.Parent.Torso.CFrame = CFrame.new(Vector3.new(84, 160, 12)) -- The character is moved
    end
end

bin.Touched:connect(onTouched)

Answer this question