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

Trying to tele player on touch but says HumanoidRootPart is not a valid member?

Asked by
tjtorin 172
7 years ago

I just want to teleport the player to its spawn when it touches a brick but I am getting the error humanoid is not a valid member of MeshPart Code:

1function touch(hit)
2    humanoid = hit.Parent.HumanoidRootPart
3    hit.humanoid.CFrame = CFrame(game.Workspace.SpawnLocation.Position)
4end
5script.Parent.Touched:connect(touch)

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

There are a few problems; let's go through them in order.

1.

:connect() is now deprecated (this may not work as expected and could be removed at any time). Instead, use :Connect(). The difference is capitalising the C.

2.

As DemonizedOverlord said, you need to check for the HumanoidRootPart before actually getting it, as it will error if you try to get something that doesn't exist.

1script.Parent.Touched:Connect(function(Hit)
2    local Root = Hit.Parent:FindFirstChild("HumanoidRootPart")
3    -- Save it to a variable so you can use it later on quickly
4 
5    if Root then
6        -- Do stuff with Root
7    end
8end)

3.

There is no need to add a hit. before using the humanoid variable (AKA Root in my code).

This will just make the code look for a child named humanoid inside hit, which isn't what you want, and will usually throw an error.

1Root.CFrame = Value

4.

You did not create a CFrame correctly. You must use the CFrame.new() function, not the library (table) it is stored in.

1script.Parent.Touched:Connect(function(Hit)
2    local Root = Hit.Parent:FindFirstChild("HumanoidRootPart")
3    -- Save it to a variable so you can use it later on quickly
4 
5    if Root then
6        Root.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position)
7    end
8end)

Hope I helped!

~TDP

0
ty tjtorin 172 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You have to check if humanoid exists otherwise it will run the function if a part touches it and it will check for humanoid in the part.

Edit: I noticed a mistake my bad.

1function touch(hit)
2if hit.Parent:FindFirstChild("Humanoid") ~= nil then
3hit.Parent.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(game.Workspace.SpawnLocation.Position))
4end
5end
6script.Parent.Touched:connect(touch)
0
A lot of things there are still wrong TheDeadlyPanther 2460 — 7y
0
yeah it does not work tjtorin 172 — 7y
0
It should now RequiredModule 38 — 7y
0
No, it still won't - you can't put Position into a Vector3, as it already is a Vector3. Also, think about 'hit.humanoid' for a second, as that is not right either. Check my answer for solutions. TheDeadlyPanther 2460 — 7y

Answer this question