Need help on NPC touch event

Hello, I am currently working on a Tower Defense like game and i’m trying to make it so that when a zombie touches a certain part my base loses health. The health is a intValue and i’m making a touch event for it. Everytime the zombie touches the part, it loses health. The problem is that it doesn’t, each zombie has a intValue inside of them saying “Enemy” and the number it supposed to damage my base. However, the touch event doesn’t work. Here is my script:

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Enemy") then
		local enemy = hit.Parent:FindFirstChild("Enemy")
		local health = game.ReplicatedStorage.Health
		health.Value -= enemy.Value
	end
end)

Is there any errors sent to console, and FindFirstChild will only get the instance, you need to check the value perhaps?

I also noticed -= whats this suppose to mean?

value1 -= value2 Is just an abbreviation for value1 = value1 - value2.

Anyways, if there were to be any problems it’d be with the if statement. Is enemy inside of the enemy model? Is it a value or the humanoid itself? Other than that the game may not be finding the health value but that’s less than likely. Try using game:GetService("ReplicatedStorage").Health instead.

The first solution would be @op to actually check the values of these values to see if they don’t come back nil or any other errors, basic debugging techniques.

Yes the enemy value is inside of the model. I will try the using game getservice to get replicatedstorage

For some reason, when I modified the code for me to walk on the part it detected me walking but when I tried it for the zombies it detected nothing and the zombies were on the part that had the touch event.

Hey guys I fixed the issue, for some reason the “can touch” setting on the zombies were turned off and i just turned it back on. This took me a whole day to realize lol.

1 Like