Repeat + until doesn't stop

I want to make a bar slowly deplete but the repeat + until statement I’m using is going into the negatives and is suppose to stop at 0.

Heres my code:

local hyperActivate = game:GetService("ReplicatedStorage"):WaitForChild("HyperActivate")

hyperActivate.OnServerEvent:Connect(function(plr)
	plr:FindFirstChild("Hyper"):FindFirstChild("HyperActive").Value = true
	repeat task.wait(1)
		plr:FindFirstChild("Hyper").Value -= 5
	until plr:FindFirstChild("Hyper").Value == 0
	plr:FindFirstChild("Hyper"):FindFirstChild("HyperActive").Value = false
end)
1 Like

Try this instead:

local hyperActivate = game:GetService("ReplicatedStorage"):WaitForChild("HyperActivate")

hyperActivate.OnServerEvent:Connect(function(plr)
	plr:FindFirstChild("Hyper"):FindFirstChild("HyperActive").Value = true

	while task.wait(1) and plr:FindFirstChild("Hyper").Value > 0 do
		plr:FindFirstChild("Hyper").Value -= 5
	end

	plr:FindFirstChild("Hyper"):FindFirstChild("HyperActive").Value = false
end)
1 Like

at “until plr:FindFirstChild(“Hyper”).Value == 0” it means that whenever it reaches 0, loop will stop, in this case it won’t work how you think it will, just simply change == to <=

1 Like

If it subtracts 5 every time, it could be skipping past 0 sometimes. Try changing == 0 to <= 0.

1 Like

The reason why it doesn’t stop is, because you want it only to end if it as 0.
If the Hyper Value would be 6, then it would go into negative so -4.

The programming uses logical math, to ignore the fact that it is in the negative you do <=, what this exactly do is that it is “Under or equal”. Having only “<” would ignore the 0 and stop only at -1.

== - Equal to
< - Smaller than
>  - Bigger than
<= - Smaller than or equal to
>= -  Bigger than or equal to
~= - Is not equal to
*=, += and  -= means like when trying to do something on numbers you dont need to extra put the number which should get subtracted. So if Value = 5 then it would work like that Value -= 1 will be 4 instead of writing Value = Value  - 1

It subtracts 10 instead of 5 somehow

Really? Can you show me a video or a screen shot? It worked for me. Also, check if anything else is subtracting it too.

You most likely subtract the value when it is below 5, making it go below 0 and therefore, not being equal to 0, causing the repeat to not stop

repeat
   task.wait(1)
   if plr:FindFirstChild("Hyper").Value > 5 then -- if the value is above 5
      plr:FindFirstChild("Hyper").Value -= 5 -- subtract by five
   else -- if it isnt above 5 (meaning it is less than 5) 
      plr:FindFirstChild("Hyper").Value = 0  -- then just make it to 0
until 
   plr:FindFirstChild("Hyper").Value == 0 -- it is now 0, so the loop stops

could be written differently, just formatted it this way to keep it a repeat until loop

It works, but its subtracting by double somehow, I doubled checked to make sure none of my other scripts were subtracting it.

Really strange, maybe just have the line that subratcts in its longer form?
In other words, change this line:

plr:FindFirstChild("Hyper").Value -= 5 -- subtract by five

To this:

plr:FindFirstChild("Hyper").Value = plr:FindFirstChild("Hyper").Value - 5

Changing it didn’t work. (minimum characters)

I’ve noticed that this block of code runs on a server event, maybe check if its being fired twice or multiple times?
Talking about this

hyperActivate.OnServerEvent:Connect(function(plr)

I put a cooldown on the event that triggers this

elseif key.KeyCode == Enum.KeyCode.H and eProg.Value == 100 and eDB == false then
		eDB = true
		evasiveActivate:FireServer()
		wait(2)
		eDB = false
	end

Im a bit stumped I’ll be honest.
A bit of a silly solution, but if it subtracts twice or by double the value, then just subtract by 2.5 instead?

Alrighty that sounds fine to me.

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