How do i get rid of this error? Attempt to perform arithmetic(add) on nil and number

It longer shows the error but as soon as the part drop it disappear

Using the place file you gave me, I get this result:

It seems like this might be an issue related to your environment?

However, I believe I know the issue you’re facing and have revised the script further to prevent this. Try out the following code within the WoodCrate script:

local newHealth = 30

script.Parent.Touched:Connect(function(hit)
	local Model = hit:FindFirstAncestorOfClass("Model")

	if Model then
		local Humanoid = Model:FindFirstChild("Humanoid")

		if Humanoid and game:GetService("Players"):GetPlayerFromCharacter(Model) then
			Humanoid.Health = Humanoid.Health + newHealth
			script.Parent:Destroy()
		end
	end
end)

Let me know if any more issues occur.

1 Like

Finally it works! So why do i need the player service? just courious

Basically we’re verifying that whatever touched the WoodCrate part is actually a player. To do this, we are using the GetPlayerFromCharacter method and passing the model that is provided to us on line 4.

If an object is returned from the method, we know that whatever touched the crate is a player and we can continue the operation.

I see… well thanks for the help! appreciate it

1 Like