Help with my loop

Hi devforum! So I have a loop, and whenever it picks 1, it changes a value to 1, and then after a bit of time, it goes back to 0. But when it gets changed to 0 from another script, the loop does not repeat again. I’ve tried using a while loop and it didn’t work. Therefore the loop only executes on the server creation, and when it gets changed to 0 via script, it stops, Please help if possible! Thanks!

local val = game.ServerScriptService.Data.BloodMoon

repeat
	if val.Value == 0 then
		local seed = math.randomseed(tick())
		local rng = math.random(1,5)
		print(rng)
		if rng == 1 then
			val.Value = 1
		end

	end
	task.wait(0.5)
until val.Value == 1

The repeat until loop is going to stop once val.Value == 1; that’s how a loop of that type works (it repeats until the condition is met).

Refactor the code into a while loop (below may not work 100%, writing off top of my head):

local rnd = Random.new()
while task.wait(0.5) do
  if val.Value == 0 then
    if rnd:NextInteger(1, 5) == 1 then
      val.Value = 1
      task.wait(0.5)
      val.Value = 0
    end
  end
end
1 Like

The loops stop completely after the condition is met. I suggest adding a val:GetPropertyChangedSignal("Value") and initialising the loop based on the value changes.

1 Like

Alright, i have an idea on what to do now, ill make this as a solution! Thanks!

1 Like

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