My for loop is avoiding the wait() in it?

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

So would I put it after the loops end?

wait no i sure that you should add a if debounce then

				for decrease = 1,5,1 do
					debounce = true
					print("Decreased")
					wait(1)
					lhit.Parent.Humanoid.Health -= 10
					
					debounce = false

Yo just set a debounce, but you didnt check it!

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

Try this and let me know.

Like that?

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
1 Like

However, set a local debounce var first

be like

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

i did

local debounce = false

The script didnt know yet the “debounce” so it will surely run out a error

ok cool. Ill try it out then and see if it works

1 Like

this time it was worse

Output

Decreased (x2036)
1 Like

I’ll hop in my studio and check it :smiley: please wait…

Well the output printed out
Decrease (x4)
Which is not a prob

C:\Users\Vatchara\Documents\Roblox Research\In making\Place with M4A1 and a humanoid.rbxl - Roblox Studio (gyazo.com)
(sorry for the weird place name, i tested in the game i currently working on)

maybe it would be better if i shared my whole script?

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:

So set a var on the first line?

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