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

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

Hmm, how many “Decreased” did it print out? Nevermind OP is probably solved

around 140 (30char30char30char)

No worries. My code’s slightly messy but feel free to message me here if you’d like to know more about what was wrong with your code and how it could be prevented in the future. Don’t forget to mark it as the solution. :slight_smile:

Also thanks everyone for all the support who posted on this topic!

2 Likes

No problem! Don’t forget to make a post when you need it!

Edit: Please mark the correct post as solved

Oh whoops. I marked the wrong post. I got it right this time

1 Like

Just a heads up, wait has been deprecated in favor of task.wait. You should be using that instead.

Wait, ‘wait()’ has been deprecated?