Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

"Humanoid is not a valid member of Player" Unsure how to fix?(+10 cookiez if you get it)

player = game.Players.LocalPlayer

cost = 500

 

function checkForColt()

    found = false

    for i, v in pairs(player.Backpack:GetChildren()) do

        if v.Name == "Colt" then

            found = true

        end

    end

    for i, v in pairs(player.Humanoid:GetChildren()) do -- This is where the error is

        if v.Name == "Colt" then

            found = true

        end

    end

    if found then 

        return true

    else

        return false

    end

 

end


script.Parent.MouseButton1Click:connect(function()

    leaderstats= player:FindFirstChild("leaderstats")

    if leaderstats then

        Money = leaderstats:FindFirstChild("Money")

        if Money then

            hasColt = checkForColt()

            if Money.Value >= cost and not hasColt then 
                Money.Value = Money.Value - cost
             NewGun = game.ReplicatedStorage.Colt:Clone() 

                NewGun.Parent = game.Players.LocalPlayer.Backpack .
          end

        end

    end

end)

The error read's Humanoid is not a valid member of Player. I've tried Charater too, with no such luck. +10 cookiez if you get it right?

1 comment

0

The 'Humanoid' I found WITHIN the Player's Character, but, sometimes the code executes BEFORE the Character has spawned, but, if it is saying for the Humanoid when you do 'Character', then that means 'Humanoid' was not existant at the time of execution. Consider using the 'WaitForChild' method?

1 answer

Your player variable refers to a Player object, which has no Humanoid. The Humanoid is found in the player's character.

You can test this in a blank place: when you select a tool, it becomes a child of the player's Character, not their Humanoid. It might be possible for the Character to be nil, so I'd recommend wrapping the 'for' loop in an 'if' statement: if player.Character then

Note that you could just use FindFirstChild instead of the for loops:

function checkForColt()
	return player.Backpack:FindFirstChild("Colt") or (player.Character and player.Character:FindFirstChild("Colt"))
end

Explanation: player.Backpack:FindFirstChild("Colt"). If "Colt" is found, FindFirstChild will return that object, or else nil. In an 'if' expression, anything that isn't false or nil counts as true. Thus, if "Colt" is found, the function will return it. If not, the or comes into play and the next part of the expression is evaluated: player.Character and player.Character:FindFirstChild("Colt"). What this does is first make sure that player.Character evaluates to true (ie is not nil). If it is nil, lua immediately stops evaluating things because the expression is nil and something, which is guaranteed to evaluate to false/nil (this is called short-circuited evaluation, if you want to read up on it). If player.Character evaluates to true, then we proceed with our check to player.Character:FindFirstChild("Colt") and return that.

Also note that it's good practice to declare your variables local (unless they need to be global), especially inside a function. Roblox lets you know of this if you open up the Script Analysis window. This has to do with scope.

This discussion is preserved as it was. New answers, comments, and votes are closed.