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

"Attempt to Index nil with Humanoid" Error?

Asked by 3 years ago
local human = game:GetService("Players")
script.Parent.MouseClick:Connect(function(player)
    local getCookies = player.leaderstats.Points
    if getCookies.Value == getCookies.Value then
        getCookies.Value = getCookies.Value + 1
    end
    if getCookies.Value <= 10 then
        return
    end
    if getCookies.Value > 10 then
        local chr = human:GetPlayerFromCharacter(player)
        local humanoid = chr.Humanoid
        if humanoid then
            humanoid.Health = 0
        end
    end
end)


So I'm trying to make so that when you click on a 'Part', it will do nothing but give you points UP UNTIL you hit 10+, if u hit 11 points you will die.

How do I correct this error?

1 answer

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

Hello,

It appears that you have incorrectly assigned your variables.

Simply replace your code with the following:

script.Parent.MouseClick:Connect(function(player)
    local getCookies = player.leaderstats.Points
    if getCookies.Value == getCookies.Value then
        getCookies.Value = getCookies.Value + 1
    end
    if getCookies.Value <= 10 then
        return
    end
    if getCookies.Value > 10 then
        local chr = player.Character or workspace:WaitForChild(player.Name) --correctly finds player's character and waits if character is unloaded
        local humanoid = chr.Humanoid
        if humanoid then
            humanoid.Health = 0
        end
    end
end)

Hope this answer helped you!

Cheers,

Chase

0
thank you! vNLegacy 9 — 3y
Ad

Answer this question