Attempt to index nil with 'WaitForChild'

I have a door lock, it checks if a value inside the player is above 250, if it is, it lets them in. It works fine. But inside, I have a kill brick which also checks if the value is above 250. It should only kill the player if their value is BELOW 250, but it still kills me even when my value is 255.

Normal script inside the kill brick:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player:WaitForChild("GroupRank").Value >= 250 then
		player.Character.Humanoid.ragdoll.Value = true
		player.Character.Humanoid.Health = 0
		game.ReplicatedStorage.Message:FireClient(player, "You shouldn't be in this room, it is for council members only!")	
	end
end)

I get this error:

attempt to index nil with 'WaitForChild'

It is on line 3.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("HumanoidRootPart") then
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player:WaitForChild("GroupRank").Value >= 250 then
		player.Character.Humanoid.ragdoll.Value = true
		player.Character.Humanoid.Health = 0
		game.ReplicatedStorage.Message:FireClient(player, "You shouldn't be in this room, it is for council members only!")	
	end
    end
end)
1 Like

If you try put print(hit.Parent) it will not show your character it show the body of the character so game.Players:GetPlayerFromCharacter will not work

You need to check if its a player, than check the value inside him

You can check whether what is hit is a player or not by using IsA() to check for a Humanoid Root Part.

In your case a good example would be,

if hit.Parent:IsA("Model") then
   if hit.Parent:FindFirstChild("HumanoidRootPart") then
       print("this is in fact a character model.")
   else
       print("This is not a character model.")
   end
end

Then proceed with waiting for it’s child.

1 Like

HumanoidRootPart is a name of a part of the body its className / instanceType is basePart…IsA wont work

1 Like

Your correct it was an error, You’d have to check if it IsA("Model") then check for a HumanoidRootPart under the Model. Brain farted LOL.

1 Like
if hit.Parent:IsA("Model") and hit.parent:FindFirstChild("HumanoidRootPart") then

end

I corrected my error in the above post, thanks again for pointing it out.

1 Like

Just change the full script so it checks for that other value aswell, like give her the full code.

This didn’t make a difference, it still killed me.

Try this,

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:IsA("Model") then
		if hit.Parent:FindFirstChild("HumanoidRootPart") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player:WaitForChild("GroupRank").Value >= 250 then
				player.Character.Humanoid.ragdoll.Value = true
				player.Character.Humanoid.Health = 0
				game.ReplicatedStorage.Message:FireClient(player, "You shouldn't be in this room, it is for council members only!")
			end
		end
	end
end)

It isn’t extremely different from his, we check if what was hit is a model.

Ngl dude, this script is too long for no reason.

the script still kills me
the error is WaitForChild, not finding the model or anything.

Is GroupRank a instance beneath the player?
Or are you trying to use Roblox’s GroupService?

It’s an int value in the player.

NOT the player character.

The path to the value:

game.Players.AdSomnum.GroupRank

Other than all the checks you have, you need 1 check

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end

Yeah you can always take those extra precautions. I do.

1 Like

Hey, are you changing the value from the server or the client, if its from the client (local Script) it will never work