Script Help for Changing a Players Location

I’m trying to make a “portal” in my game to “teleport” the player to a chapter selection area.
I’m using the following script to change their position:

local portal = script.Parent

function onTouch(player)
	player.Parent.Transform.Position.Origin = Vector3.new(-133.879, 18.432, -192.058)
end

portal.Touched:Connect(onTouch)

But I keep getting this response:
image

I’m not sure what I’m doing wrong. I appreciate any help!

You would have to change:

player.Parent.Transform.Position.Origin

to

player.HumanoidRootPart.Position
1 Like

That doesn’t work because the “player” represents whichever body part touches it first:
image
With the parent it says this:
image

That’s because the scritp thinks that the HumanoidRootPart is the children of LeftHand, I don’t know why it’s referencing it like that, is there anything more of the scritp?

No – For some reason whenever I do a touch event it refers either to an accessory or body part. Then I have to do “player.Parent” in order for it to refer to the player rather than the part.

For example:


Even after I say “.Parent”

Change this to Vector3.new(-133.879, 18.432, -192.058)

Vector3.new()

you’re mising the .new
also, just do player.Position no need for transform nor origin

I already added .new – still not working. It keeps referring to an accessory or body part rather than the full Humanoid (when the player touches the part)

Here is a more robust solution which ensures that what is touching the block is a player (generally) and does not error. It is also what you are looking for:


local Players = game:GetService("Players")

local portal = script.Parent

local DESTINATION   = Vector3.new(-133.879, 18.432, -192.058)

local function onTouched(hit)
    local humanoid = hit.Parent and hit.Parent:FindFirstChildWhichIsA("Humanoid")
    if humanoid and humanoid.RootPart then
        humanoid.RootPart.Position = DESTINATION
    end
end

portal.Touched:Connect(onTouched)

If you want to ensure it’s a player and not just e.g. an NPC with a humanoid inside of them, make use of game:GetService("Players"):GetPlayerFromCharacter(character).

1 Like

Yep, that fixes the problem. Thank you!!!

That’s because you have to do, hit.Parent, not player.Parent

1 Like

Wouldn’t they both be the same? Player is just the variable.

1 Like

No, because the parent of the player instance is the Players service, and hit is the part that hits the part, in this case LeftHand, so you would have to do this:

hit.Parent.HumanoidRootPart.Position
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.