How do I break the loop once the value is false?

local EffectEvent = game.ReplicatedStorage.Effect
local players = game.Players
local char = script.Parent.Parent.Parent.Parent
local Humanoid = char:WaitForChild("Humanoid")
local BleedVal = script.Parent.Parent.Parent.Parent.Effects.Preset.Bleeding

local dmgOverTime = 5
local dmgTime = 5
local dmgCD = 3

BleedVal.Changed:Connect(function(val)
	EffectEvent:FireClient(players:GetPlayerFromCharacter(char), val, BleedVal)
	while val do
		task.wait(dmgCD)
		dmgTime -= 1
		Humanoid:TakeDamage(dmgOverTime)
	end
end)

I use a .Changed event to find the value, however it appears that the loop does not stop itself once the value changes.

Loops can be broken using the keyword: break
How do you want to break the loop? Can you explain a bit further?

As seen in the script, the while loop is:

while val do

which means, that as long as the value == true, then the while loop should technically run.

However, despite the value being disabled, it still runs the loop and does not break it.

I don’t know much about lua. I don’t know why am I trying to help you, but I can try, I guess…!

local EffectEvent = game.ReplicatedStorage.Effect
local players = game.Players
local char = script.Parent.Parent.Parent.Parent
local Humanoid = char:WaitForChild("Humanoid")
local BleedVal = script.Parent.Parent.Parent.Parent.Effects.Preset.Bleeding

local dmgOverTime = 5
local dmgTime = 5
local dmgCD = 3
local shouldContinue = true

BleedVal.Changed:Connect(function(val)
    EffectEvent:FireClient(players:GetPlayerFromCharacter(char), val, BleedVal)
    shouldContinue = val
    while shouldContinue do
        task.wait(dmgCD)
        dmgTime -= 1
        Humanoid:TakeDamage(dmgOverTime)
        if dmgTime <= 0 then
            shouldContinue = false
        end
    end
end)

I did some changes to make it work. shouldContinue is set to val when the BleedVal changes, and then the loop checks this variable to decide whether to continue or not. Maybe it will help. The loop also checks if dmgTime has reached 0, and if so, sets shouldContinue to false to stop the loop. It can be wrong, let me know if it does not work.

val won’t change when BleedVal.Value changes.
You’ll need to check if the value of BleedVal has changed every time the loop runs, like this:

BleedVal.Changed:Connect(function(val)
	EffectEvent:FireClient(players:GetPlayerFromCharacter(char), val, BleedVal)
	while BleedVal.Value do -- while the value of BleedVal is true
		task.wait(dmgCD)
		dmgTime -= 1
		Humanoid:TakeDamage(dmgOverTime)
	end
end)

It looks like you are making some kind of damage due a Variable. I do not recommend doing it by a Changed function, you can directly make a loop which goes everytime or in the changed function you could just instead of doing what you did, do this:

while BleedVal == val do

end

May I ask why? (KEY KEY KEYEEE EKEKE)

This worked very well, albeit the loop just wants to get that last bit of damage in.
I could conteract that by instead of making the loop depend on the value, just put an if statement with a break

1 Like

Sorry, ignore the first part. Don’t do a loop, it depends on the system.
About the second part though.

while BleedVal.Value ==  val do

end

As you can clearly see, it checks if the BleedVal.Value is still the same like the one when it got changed. The Changed function only sets it value once and never again, as it is a loops once it changed and can’t change it value again.

1 Like

Ah, I see now. Thank you!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.