Debounce loop makes it so loop does not stop

You can write your topic however you want, but you need to answer these questions:

  1. When I touch a part, it should play a sound and start taking damage, but when you leave the part it stops.

  2. It does not stop the sound and damage if debounce is in there.

local touching
local db = true

game.Workspace.Part.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		touching = true

		if db == true then
			db = false
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			while touching == true do
				player.Character.Humanoid:TakeDamage (1)
			player.Character.Health.Disabled = true
				game.Workspace.Sound:Play()
				wait(10)
				db = true

				end
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				end
			end
		end)






game.Workspace.Part.TouchEnded:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		touching = false

		if db == true then
			db = false

			print("test")
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			hit.Parent.Humanoid:TakeDamage (0)
			player.Character.Health.Disabled = false
			game.Workspace.Sound:Stop()			
			db = true

		end
	end
	end
	)
	




1 Like

Check if the sound is set to loop

Tried, that didnt work. I remove all of the debounce stuff it works but I need that or it will continuously take damage everytime they move

For a debounce, i use a Value inside a char. I Name it « Cooldown » and just delete it after the time is done. Then i check if the target has the value. If yes then it return end