Player taking damage after time

Hi guys,I scripted a part that damage the player,I wanted to damage the player after a bit of time
so I added something to the script
This is the original script

– code
local Debounce = false

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and Debounce == false then
	    
	    Debounce = true
		hit.Parent.Humanoid:TakeDamage(35)
	  wait (1)	
	  Debounce = false
	end
end)

This is the script that I edited to damage the player after a bit of time

– code
local Debounce = false

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and Debounce == false then
	   wait (6) 
	    Debounce = true
		hit.Parent.Humanoid:TakeDamage(35)
	  wait (1)	
	  Debounce = false
	end
end)

As you can see I just added a wait command,but I wanted to know if this is the right way to do it

A couple thing’s I’d change:

  1. This is a serverside debounce, meaning if multiple people touch it, only 1 player can take damage per debounce. A simple solution is by changing the player’s HumanoidRootPart color (it’s already invisible), which creates a “local” debounce.
  2. You had 2 waits, so I reduced it to only 1 (that happens after the damage is taken)
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.HumanoidRootPart.BrickColor == BrickColor.new("Medium stone grey") then
        hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Really red")
        
        hit.Parent.Humanoid:TakeDamage(35)
        wait(1) --time until debounce is over

        hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Medium stone grey")
    end
end)

I can see that in this script you used the brickcolor.new comand,but the part that will damage the player is invisible so I dont need to change its color,do I have to ignore that comand or it is a fundamental part of the script and I misread it?

The only reason I used BrickColor.new on the HumanoidRootPart is:

  1. It’s invisible
  2. It will create a “local” debounce, meaning multiple people can touch the part at once and all take damage from it.

It’s not theoretically a fundamental part, but if you want all players that are touching the part to take damage, then yes, it is.

Ok thank you you for clarifying

You may want to mark a solution so other people know that this question has been solved

Just a question,in the script you used only one wait comand after the damage is taken,but I wanted to damage the player after a certain time that they are inside of the part

Oh, if so, you would just move the wait in front of the damage part, as so:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.HumanoidRootPart.BrickColor == BrickColor.new("Medium stone grey") then
        hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Really red")
        
        wait(1) --time until debounce is over and player takes damage
        hit.Parent.Humanoid:TakeDamage(35)

        hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Medium stone grey")
    end
end)

This single wait(1) that occurs before the damage acts as both a debounce and a time until taking damage

1 Like