Debouncer Wont prevent Touched from firing multiple times

  1. The code is supposed to gradually heal the player over time as long as they stand on a designated platform.

  2. Rather than heal in consistent increments, the debouncer keeps failing and allowing a random amount of heals to generate each time the loop restarts.
    hmm

  1. I have tried a variety of different debouncer techniques, most of which just crash the game outright due to “timeouts.” I have found articles with similar problems, but none of the solutions were related to this problem.
local health = game.ReplicatedStorage.PlayerStats.Health
local Healer = script.Parent
local debounce = true



Healer.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)	
	while health.Value < 100 do
			if plr and debounce then
				health.Value = health.Value + 1
				debounce = false
				print("Healed")
				else
				print("Stop Moving!")
		end
		wait(5)	
		debounce = true
	end
end)
1 Like

Try this script put it inside the part the only problem i have with doing this is that the touched event is not accurate as your character cant be static and must be moving in some way even through an animation, It’s like a Venus fly trap.

and the debounce means multiple players cannot heal at once.

function addToHealth(addAmount, humanoid)
	local addingToHealth = math.clamp(humanoid.Health+addAmount, 0, 100)
	humanoid.Health = addingToHealth
end

local canHeal = true
local waitTime = 4
local part = script.Parent

part.Touched:Connect(function(hitPart)
	if not hitPart.Parent:FindFirstChild("Humanoid") then
		if not hitPart.Parent.Parent:FindFirstChild("Humanoid") then print('no') return end
	end
	
	local humanoid = hitPart.Parent:FindFirstChild("Humanoid") or hitPart.Parent.Parent:FindFirstChild("Humanoid")
	
	if canHeal then
		canHeal = false
		
		while humanoid.Health < humanoid.MaxHealth do
			addToHealth(25, humanoid)
			print('healing ',game.Players:GetPlayerFromCharacter(humanoid.Parent))
			wait(waitTime/2)
		end
		wait(waitTime/2)
		
		canHeal = true
	end
end)
1 Like

This doesn’t seem to trigger anything, but I will play around with it in case it is something on my end. Thank you so much for the advice.

how so? is it a server script you put in the part?

1 Like

I got it to work. I appreciate the help. And yes. It was ran through a server script.

1 Like

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