Attempt to perform arithmetic nil Percentage damage!

I Gave humanoids the percentage attribute and replaced the take damage function with the percentage changed and for some reason it will no longer run

here is the percentage attribute assignment

game.workspace.ChildAdded:Connect(function(child)
	if child:FindFirstChild('Humanoid') then
		child:SetAttribute('Percentage',0)
	end
end)
for _, v in pairs(workspace:GetDescendants()) do
	if v:FindFirstChild('Humanoid') then
		v:SetAttribute('Percentage',0)
	end
end

this seems to work the attribute is apart of the player when the game is run but the new damage function returns me a not so nice error

ServerScriptService.Knockback:6: attempt to perform arithmetic (add) on nil and number

here it is

if data.Action == "m1" then
		data.Target.Humanoid:GetAttribute('Percentage' ) 
        data.Target.Humanoid:SetAttribute('Percentage', data.Target.Humanoid:GetAttribute('Percentage') + 2)

For some reason that block of code won’t work
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I’d add some debug prints and check for nil values.

Attribute Assignment Script:

game.Workspace.ChildAdded:Connect(function(child)
    if child:FindFirstChild('Humanoid') then
        child:SetAttribute('Percentage', 0)
        print("Set Percentage to 0 for: " .. child.Name)
    end
end)

for _, v in pairs(workspace:GetDescendants()) do
    if v:FindFirstChild('Humanoid') then
        v:SetAttribute('Percentage', 0)
        print("Set Percentage to 0 for: " .. v.Name)
    end
end

Damage Function Script:

if data.Action == "m1" then
    local currentPercentage = data.Target.Humanoid:GetAttribute('Percentage')
    
    if currentPercentage == nil then
        -- This shouldn't happen if attributes are set correctly
        warn("Percentage attribute is nil for: " .. data.Target.Humanoid.Parent.Name)
        currentPercentage = 0
        data.Target.Humanoid:SetAttribute('Percentage', currentPercentage)
    end
    
    data.Target.Humanoid:SetAttribute('Percentage', currentPercentage + 2)
    print("Updated Percentage for: " .. data.Target.Humanoid.Parent.Name .. " to " .. (currentPercentage + 2))
end

Let me know what is outputted!

The output is that the current percentage is nil from the warn you added

while it prints an updated percent the actual percentage remains 0 in the attribute

Is the Percentage attribute only for players? Is the code being run in a server or local script?

The percentage damage script is being run on a local script while the percentage assignment is in a server script

And i’m testing the script on an npc in workspace that has the attribute

Would you be comfortable with sharing the place file?

How would I do that? and yeah I’m fine with it

1 Like

Save the place as a file (something like .rblx) to your desktop upload to the forum. Should be able to drag and drop it.

Place.rbxl (107.3 KB)

Looks like you were mixing up the Humanoid and the actual Character object. This should be what you want. (i.e you were setting the Attribute on the wrong object)

Let me know if this fixes your issue?
Place.rbxl (106.8 KB)

Works and I understand the problem I didn’t set it to the humanoid but the character.

1 Like