KillBrick working but Damage Kill brick not working


local KillBrick = workspace.KillBrick
KillBrick.Touched:Connect(function(touched)
local humanoid = touched.Parent:FindFirstChild(“Humanoid”)
if humanoid then
humanoid.Health = 0
end
end)

local DamageKillBrick = workspace.KillBrick
DamageKillBrick.Touched:Connect(function(touching)
local humanoid = touching.Parent:FindFirstChild(“Humanoid”)
if humanoid then
humanoid.Health = 0.5
end
end)

Instead of using humanoid.Health = 0.5, minus it instead: humanoid.Health -= 5

1 Like

nope, unfortunately that is not working

Try this:

local DamageKillBrick = workspace.DamageKillBrick -- you need to reference the correct part
DamageKillBrick.Touched:Connect(function(touching)
	local humanoid = touching.Parent:FindFirstChild(“Humanoid”)
	if humanoid then
		humanoid.Health -= 5
	end
end)
1 Like

Oh yes! Great thank you, may i ask what was different?

Oh i just realised i did the wrong variable!

You referenced the DamageKillBrick variable as workspace.KillBrick.

I’d like to inform you of a more performant way to create a kill/damage part. You can use CollectionService to bind the same code to a multitude of parts. Additionally you can look up a tutorial to comprehend the service.

Side stuff

Here’s some optimized code which contains some additional variables for configuration:

-- services
local players = game:GetService("Players")

-- variables
local damageKillBrick = workspace:WaitForChild("DamageBrick")

local debounces = {}

local COOLDOWN_LENGTH = 0.75
local DAMAGE = 20

-- functions
damageKillBrick.Touched:Connect(
	function (otherPart)
		local player = players:GetPlayerFromCharacter(otherPart.Parent)
		
		if (not player) then return end
		if (table.find(debounces, player.UserId)) then return end
		
		table.insert(debounces, player.UserId)
		
		local character = otherPart.Parent
		local humanoid = character:WaitForChild("Humanoid")
		
		humanoid.Health -= DAMAGE
		
		task.delay(COOLDOWN_LENGTH,
			function ()
				table.remove(debounces, table.find(debounces, player.UserId))
			end
		)
	end
)

You can mark my reply as the solution if I’ve solved your problem, good luck :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.