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

"Attempt to Index nil with Humanoid" Error?

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

✓ Accepted answer

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

1 comment

0

thank you!

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