hey, im trying to make a “bleeding system,” where if a player touches a sharp object(in this instance, barbed wire) they would start bleeding.
The issue I have is when the “bleed” effect starts, touching the barbed wire can re-run the loop, causing the loop to run multiple times. How can I prevent this?
code:
local BarbedWire = script.Parent
local Curve = BarbedWire.Curve
local Debounce = false
Curve.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not Debounce then
Debounce = true
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 1
task.wait(1)
Debounce = false
local BleedChance = math.random(0, 100)
local BleedTimer = math.random(5,15)
local BleedEnabled = false
print("bleedchance:", BleedChance)
if BleedChance >= 85 then
if not BleedEnabled then
BleedEnabled = true
while BleedTimer do
BleedTimer = BleedTimer - 1
print("bleedtimer:", BleedTimer)
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 3
task.wait(1)
if BleedTimer == 0 then
break
end
end
BleedEnabled = false
end
end
end
end
end)
I just found out the issue, it has to do with debouncing
however this fix prevents the humanoid.health remove line from running until the loop is done, if enabled.
any ideas?
Curve.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not Debounce then
Debounce = true
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 15
task.wait(2)
-- Debounce = false -- if this debounce is here, the loop will stack
-- multiple times
local BleedChance = math.random(0, 100)
local BleedTimer = math.random(5,15)
if BleedChance >= 15 then
if not Debounce2 then
Debounce2 = true
for i = BleedTimer, 1, -1 do
Debounce2 = false
BleedTimer = BleedTimer - 1
print("bleedtimer:", BleedTimer)
task.wait(1)
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 3
if BleedTimer == 0 then
print("done")
end
end
end
end
Debounce2 = false
Debounce = false -- down here prevents the stacked loop
end
end
end)
Your idea of the debounce system is preventing other players to get affected by the object.
You can use CollectionService to add a tag in a target’s humanoid, and if they touch the part while they have a tag, it would not have any effects on them.
After the bleeding is done, you may remove the tag.
You are mainly looking for the following functions: :AddTag() :RemoveTag() :HasTag()