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

Ok, so just add a local debounce = false
on a line 1

Because you are resetting a debounce everytime player touchs which messed up a script

so should i check if it has been touched by somthing with a humanoid in it. then debounce it until the loop is done. then it resets

Just change a local debounce = false line to the first line

What are you trying to accomplish? That code is creating 50 LB.Touched connections…

Please use another debounce for the touched event like this:

local debounce2 = false

script.Parent.Parent.TouchPart.Touched:Connect(function(hit)
	if debounce2 == false and hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Wall") then
            debounce2 = true
			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)
        debounce2 = false
	end
end)

Add a second debounce variable, check for debounce == false when touched even happened, then set debounce to true when it returns true, then set debounce to false when the whole script is done.

well. when a character touches a certain part. it creates a clone of a part (beam) and it moves to your character’s position every 0.05 seconds. (line 11) then every time you touch the beam it decreases the player’s health. may sound confusing

Every 0.05 seconds? Only for 50 iterations or do you want it to move to the Character’s position forever?

so one loop (line 11) makes the part go to the player’s pos. Then the other loop works on decreasing the player’s health when they are touching the beam

nope. I get this

Decreased (x2332)

And does it only deal damage to that player that the beam is following?

Nevermind, I just noticed you are using 2 Touched events on your script. For that you would have to add another debounce on the LB.Touched function.

well when the player is touching the beam but yes

Okay, I don’t really know what’s the problem here, but try this maybe?

if lhit.Parent.Humanoid == nil then 
					return
				else
					for decrease = 1,5,1 do
						debounce = true
						print("Decreased")
						lhit.Parent.Humanoid.Health -= 10
						
                        wait(1)
						debounce = false
					end
				end

nope, that doesn’t work. (30char)

Hmmm, put the debounce outside the for loop? (My small brain idea)

Here is a cleaner script:

local DB1 = false
local DB2 = false

script.Parent.Parent.TouchPart.Touched:Connect(function(hit)
   if DB1 == false and hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Wall") then
      DB1 = true
      local HitAnim = script:FindFirstChild("Animation")
      local distance = 30			
	
      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
               if DB2 == false then
                  DB2 = true
                  for decrease = 1,5,1 do
                     print("Decreased")
                     wait(1)
                     lhit.Parent.Humanoid.Health -= 10
                  end
               end
            end
         end)

         LB.Transparency = 0			
         wait(0.05)
         DB1 = false
         DB2 = false

      end
   end
end)


Sorry I forgot to include something, I just edited the post.

1 Like
local LightB = game.ReplicatedStorage:WaitForChild("LightBeam")
local HitAnim = script:FindFirstChild("Animation")
local TouchPart = script.Parent.Parent:WaitForChild("TouchPart")
local distance = 30
local debounce = false

local function Touched(LB,Humanoid)
	local debounce1 = false
	return LB.Touched:Connect(function(hit)
		if Humanoid.Parent == hit.Parent and not debounce1 then
			debounce1 = true
			for decrease = 1,5,1 do
				print("Decreased")
				Humanoid.Health -= 10
				wait(1)
			end
			debounce1 = false
		end
	end)
end

TouchPart.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	if Humanoid and not Character:FindFirstChild("Wall") and not debounce then
		debounce = true
		
		coroutine.wrap(function()
			local LB = LightB:Clone()
			LB.Parent = script.Parent --Should this not be workspace?
			Touched(LB,Humanoid)
			
			LB.Transparency = 0
			
			for count = 1,50,1 do
				task.wait(0.05)
				LB.Position = hit.Parent.Torso.Position
			end
			
			LB.Transparency = 0	--Do we not destroy LB?	
		end)()
		
		task.wait(0.5)
		debounce = false
	end
end)

With the information you gave me, I came up with this really quickly. Test it out and let me know what happens since I couldn’t really test it.

Edit: Fixed something.

1 Like

when i started touching it it lagged a whole ton

So I tried it out. and it works! Thank you lots!

1 Like