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

Why is the player not teleporting when I use CFrame?

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

✓ Accepted answer
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)

2 comments

0

It works! Thank you!

0

No problem, glad I could help.

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)

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