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:
I’m not sure what I’m doing wrong. I appreciate any help!
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.
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).
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: