"not a valid member of something" even though its there in the workspace?


As you can see here, it says HumanoidRootPart is not a valid member of Scout, even though in the workspace its there

Basically this happens after I clone the object ( its like when in a tower defense game, you have to upgrade towers. And this occurs after upgrading the tower once, it works before upgrading )

function findClosestEnemy(fromTower)
	for i, v in pairs(game.Workspace.Enemies:GetChildren()) do
		local distance = (v.HumanoidRootPart.Position - fromTower.HumanoidRootPart.Position).Magnitude
		if distance < 15 then
			--print("Shoot")
			return v
		end
	end
end

it errors here, even though this same function works before cloning ( upgrading the tower ).

Have you ensured that it’s actually there? Try printing all of the children of the enemy to see if HumanoidRootPart actually exists to the script

1 Like

HumanoidRootPart is not attached anymore once cloned, so it will basically remove.

1 Like

Maybe the tower or it’s HumanoidRootPart is falling out of the world, try anchoring it.

1 Like

Oh wow, I printed the children of the enemy, it shows NOTHING in the table???

It is weird, there are body parts in the tower showed in the workspace, but it prints nothing is in the tower when I print all children of the tower

That must mean that they arent cloning correctly, is the findClosestEnemy function in a local script?

1 Like

nope, it is in a server script

The script could be running before the children of the enemy has loaded in the game

try to add this line to wait until the HumanoidRootPart exists (put it before the line where you defined the distance variable)

repeat wait() until v:FindFirstChild("HumanoidRootPart")
1 Like

are you cloning the enemies on the client? if so the server won’t know they exist

^ try doing what @VeriBaesix said

1 Like

I tried this, now it just waits forever. Somehow the script can’t find the humanoidRootPart even though its right there.

try checking your explorer to see if they exist using the server view

1 Like

It does exist in server view and player.

Can you show us the Enemies inside the explorer while in playtesting mode?

1 Like


There are tons of children in it, and yet the script prints nothing is in the model Scout

Wait I found out the problem, when I cloned it, I only set the torso’s position and body parts, I didn’t set the humanoidRootPart’s position. Maybe thats why it bugs.
Now it works fine ( since I heard you have to set the torso’s position if you are dealing with r6 characters )

2 Likes