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

I was working on a script that would allow the part to give health points to players but apparently this error
broke the script(you can take a look at the video to know what im talking about):

Here’s the script for it aswell

script.Parent.Touched:Connect(function(hit)
	local player = hit.Parent.Name
	local newHealth = 30
	if player then
		player.Health = player.Health + newHealth
	end
	script.Parent:Destroy()
end)

Consider changing

local player = hit.Parent.Name

To:

local player = hit.Parent.Humanoid

You where trying to edit the Health of the players name. Health is not a property of a string.

1 Like

Is there something wrong with this line too?


player.Health = player.Health + newHealth

Let me take a look, I have to reload studio so it might be a few minutes

Game.rbxl (43.7 KB)

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
	local new health = 30
	if humanoid then
		humanoid.Health = humanoid.Health + newHealth
	end
	script.Parent:Destroy()
end)

Your mistake was that you were giving more life to the player’s name, instead of the Humanoid which has Health as its property. :slight_smile:

You should make sure you’re actually referencing the Humanoid when changing health. It looks like you’re just trying to set the health of the string hit.Parent.Name.

Here’s a revised bit of code to hopefully ensure that you are referencing the player’s humanoid:

local newHealth = 30

script.Parent.Touched:Connect(function(hit)
	local Model = hit:FindFirstAncestorOfClass("Model")
	
	if Model then
		local Humanoid = Model:FindFirstChild("Humanoid")
		
		if Humanoid then
			Humanoid.Health = Humanoid.Health + newHealth
			script.Parent:Destroy()
		end
	end
end)

This code ensures that a humanoid exists before trying to perform any kind of math on the health of the object.

Now its saying Humanoid is not a valid member of Model workspace.DesertMap

What line of the following code is it yielding that error?

local newHealth = 30

script.Parent.Touched:Connect(function(hit)
	local Model = hit:FindFirstAncestorOfClass("Model")
	
	if Model then
		local Humanoid = Model:FindFirstChild("Humanoid")
		
		if Humanoid then
			Humanoid.Health = Humanoid.Health + newHealth
			script.Parent:Destroy()
		end
	end
end)

It’s odd to receive an error like that when I’m performing checks to prevent them.

Line 2 gives an error according to the output

I’m a bit confused. There’s nothing on line 2 as you can see. Are you sure you are using the exact code I gave you?

image

And yes i used the code you gave me

Can you send me the code you are using in your script?

can i send you the file instead?

Sure. I can take a look for you.

Game.rbxl (43.8 KB)

I just loaded the place file and it worked exactly as intended. I’m not sure how you’re receiving that error as there’s nothing on Line 2 of the WoodCrate script and it runs fine for me.

Did you use the console on the roblox laucnher?

I created a two player testing session with the place file you provided to me. Did you publish that place file to the website when testing it on the Roblox client?