Continuous touch damage

So basically I want to make player deal damage continuously when they touch the part.

However, using the touched function.

script.parent.Touched:Connect(function(part)

Local player = part.Parent
Local humanoid = player.Humanoid
humanoid.Health -= 1

end)

The player will only get detected for a few times and deal ~ 4-7 damage
Any ways to make the part keep detecting if the player still on the part and damage the player continuously?
Thank you :pray:

you need to use a debounce more info about it here if you want to understand it more

   local db = false
    local DB_TIME = 4.5 -- 4.5 seconds
    script.parent.Touched:Connect(function(part)
        if db then return end -- makes it not run
        db = true
        Local player = part.Parent
        Local humanoid = player.Humanoid
        humanoid.Health -= 1
        wait(DB_TIME) -- waits a time then turns off the debounce
        db = false
    end)
2 Likes

OP wanted continuous damage? I think you would need a smaller debounce, maybe around .5 or 1 second.

3 Likes

Yeah second thought since he said

β€œAny ways to make the part keep detecting if the player still on the part and damage the player continuously?”

im now thinking he should use Region3 to do this

1 Like

just turn off the CanTouch value

script.Parent.Touched:Connect(function(part)
    local player = part.Parent
    local humanoid = player.Humanoid
    humanoid:TakeDamage(1)
    script.Parent.CanTouch = false
    wait(1)
    script.Parent.CanTouch = true
end)
3 Likes

Good idea, could be advanced for beginners
@OP, read more about Region3 here.

Region3 | Roblox Creator Documentation.

2 Likes

Okay here im pretty sure this is what you wanted you can also read more about region3 on the roblox wiki like @THECOOLGENERATOR said

local DamageTime = 1 -- amount of time until next check
local DamageAmount = 5 -- how much damage per check
local Part = script.Parent -- the part


local function GivePlayersInArea()
	local region = Region3.new(Part.CFrame.p-Part.Size/2,Part.CFrame.p+Part.Size/2)
	local parts = workspace:FindPartsInRegion3(region,nil,math.huge) -- gives all the part in the region3 i picked math.huge so that means we can have no limit on how many parts can be in the region
	local tab = {}
	for i,v in pairs(parts) do
		if v.Parent then
			if game.Players:GetPlayerFromCharacter(v.Parent) and not table.find(tab,v.Parent) then
				table.insert(tab,v.Parent) -- puts the players character inside the table
			end
		end
	end
	return tab
end

while wait(DamageTime) do
	for i ,v in pairs(GivePlayersInArea()) do
		v.Humanoid:TakeDamage(DamageAmount) -- takes damage
	end
end
6 Likes