How to create a damage brick in roblox studio?

I have a folder of damage bricks that I want to deal a certain amount of damage to each player upon contact. I made a script for them, and it works, but for whatever reason it doesn’t damage you when you aren’t moving (and making contact with the brick at the same time obviously). I tried looking up solutions for this, and I haven’t found anything that is relevant or that worked for me. My game is r6, and yes, I want it to damage the player regardless of if they have their spawn forcefield on.

The script I currently have is shown below:

local KillBricks = script.Parent

local Damage = 10
local ResetTime = 1

local Debounce = false

for _,v in pairs(KillBricks:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if Debounce == false then
				Debounce = true
				if hit.Parent:FindFirstChild("Humanoid") then
					hit.Parent.Humanoid.Health -= Damage
				end
				wait(ResetTime)
				Debounce = false
			end
		end)
	end
end

My killbrick folder is shown below:
image

Any help would be appreciated!

2 Likes

The touched event fires even if another part hits it, so perhaps its somehow colliding with something else and constantly being caught on the debouce. Try making it so the debounce only occurs, when the thing it hits, is a player.

if Debounce == false then
	if hit.Parent:FindFirstChild("Humanoid") then
        Debounce = true
		hit.Parent.Humanoid.Health -= Damage
        wait(ResetTime)
		Debounce = false
	end
end

Touched is only fired when contact is made between a physically simulated object with another; the state may be constant but the event is not. You will have to attach a loop here that will continue the damage cycle while the player is still touching (or on top of/in) a damage brick.

FWIW: your debounce is global so if one brick’s Touched event turns off the debounce, it’ll also affect all other blocks, unless that’s intended behaviour for your setup.

I highly recommend using a table to store debounces, because if one kill part gets touched by something random all of the kill bricks will not work. You will also want to check what players have recently touched the kill brick and then add a debounce to them. This should fix most of the issues in the script.

Ohh that makes more sense now.

How exactly would I do that? I would have to store the debounces, but in what form? How would I apply the debounce to everything in the table equally (and at the same time)?

There is a better method that does not even require debounces, which I do not recommend using in the first place. You can just set up .Touched Events to manage the lava and then check if its a player/npc and if it is, set their humanoid.health to 0.

1 Like

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