Subtract Health Brick Not working

Trying to make a brick that subtracts 10 from your health if you touch it:

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		h.Health - 10
	end
end)
	

Use a compound operator to subtract the health.

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		h.Health -= 10 -- "-=" instead of just "-"
	end
end)

Edit: Also, if you only want it to subtract 10 health you might need to add a debounce since the .Touched event usually fires ~5 times a second while the part is being touched (leading to near instant death)

1 Like

How do you add debounce?

Sorry I new to scripting

Something like this

local deb = false

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h and not deb then
		deb = true
		h.Health -= 10 -- "-=" instead of just "-"
		wait(1)
		deb = false
	end
end)

Makes you take damage every second, increase the time in the wait to make it wait longer before you can be damaged

4 Likes

Apparently LUA focuses so much on making things equal I just realized

Supposedly that would work, but since coding is different it would result as an error since it’s not a valid statement

A debounce is basically just a cooldown, you can learn more about it here:

1 Like

use a humanoid function that subtracts health called :TakeDamage()

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		h:TakeDamage(10)
	end
end)
1 Like