Hi there, so I’m having some trouble with a for loop. The script is supposed to decrease Player Health when touching.
Part of the main Script
if lhit.Parent.Humanoid == nil then
return
else
for decrease = 1,5,1 do
debounce = true
print("Decreased")
wait(1)
lhit.Parent.Humanoid.Health -= 10
debounce = false
end
end
Output
Decreased (x803)
I would be happy to share any more scripts or information.
remember to add a wait after a loop too!
(i did see you add only wait(1) but for sure, you need to add a another wait())
not pretty sure, but mine exactly worked
Neatness aside, I believe this is a result of an improper usage of debounces in a .Touched connection. Assuming you need a global debounce, instead of a per-player debounce, this is what I’d suggest doing:
if lhit.Parent.Humanoid == nil or debounce then
return
else
debounce = true
for decrease = 1,5,1 do
print("Decreased")
wait(1)
lhit.Parent.Humanoid.Health -= 10
end
debounce = false
end
for decrease = 1,5,1 do
if debounce == false then
debounce = true
print("Decreased")
wait(1)
lhit.Parent.Humanoid.Health -= 10
debounce = false
end
end
It’s because they’re using a .Touched event. Not just the for-loop. There are many for-loops running at once because the debounce hasn’t been set properly for a .Touched event. That’s why I suggested this:
script.Parent.Parent.TouchPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Wall") then
local HitAnim = script:FindFirstChild("Animation")
local distance = 30
local debounce = false
local LightB = game.ReplicatedStorage.LightBeam
local LB = LightB:Clone()
LB.Parent = script.Parent
LB.Transparency = 0
for count = 1,50,1 do
wait(0.05)
LB.Position = hit.Parent.Torso.Position
LB.Touched:Connect(function(lhit)
if lhit.Parent.Humanoid == nil then
return
else
local debounce = false
for decrease = 1,5,1 do
if debounce == false then
debounce = true
print("Decreased")
wait(1)
lhit.Parent.Humanoid.Health -= 10
debounce = false
end
end
end
end)
end
LB.Transparency = 0
wait(0.05)
end
end)
I didn’t share the whole script because I thought it wouldn’t make sense. But here it is