Getting a repeat loop to activate again

So, I am making a system for a steam train for the water level to lower until it hits 0. Right now I am using a repeat until loop to make sure it stops and doesn’t go into the negatives. The problem I have though, is getting it to start again when the water level rises up again. Repeat loops can’t be, like, turned on again from what it seems?

Any help? Here is the current code:

function waterloss()

wait(1)

Water.Value=Water.Value-2

end

repeat

waterloss()

until Water.Value<=2

Any help is appreciated, thanks

I don’t think there is really a way to do this without not yielding the codde, you could however do this:

task.spawn(function() -- so it doesnt yield code
    while true do
       if Condition then
          -- code
       end
    end
end)

Or, a function

function thing()
  while Conditon do wait()
     -- code
  end

end

Also, this code isn’t responsible for raising the water again. That is seperate.

What does task.spawn do? Sorry lol i am not the most educated on all these

To put it Simply, It fires code immediatley without yielding code

Try replacing the function that changes your water level with this

function changeWaterLevel(level, loops, length)
	length = length/loops
	loops = 1/loops
	
	while true do
		wait(length)
		Water.Value = Water.Value * (1-loops) + level * loops
		if math.floor(Water.Value) == level then
			Water.Value = math.floor(Water.Value)
			break	
		end
	end
end

This will smoothly change Water.Value to the provided level (level) over the amount of loops (loops) you specify in the amount of time you specify in seconds (length). This means you can use it to both take away and add water. Using this you should never have to repeatedly check if its going into the negatives or not.

Make sure to close this post by marking it solved if it works

I uhh tried to define loops and level
image
How does this work?

Water.Value must not be defined somewhere. Give me one second though so I can add a part to the function that lets you decide how long itll take to reach that value.

Updated my first reply with the correct function. Try it now while is Water.Value defined

Yes Water.Value refers to a numbervalue

This is the whole thing if you need it.

local TweenService = game:GetService(“TweenService”)
local RunService = game:GetService(“RunService”)
local MainDetail = script.Parent
local WaterGuage = MainDetail:WaitForChild(“WaterGauge”)
local Locomotive = script.Parent.EngineerLocal.Train.Value
local Water = Locomotive.Parent.Parent:WaitForChild(“Tender”):WaitForChild(“WaterInlet”):WaitForChild(“WaterLevel”)
local LocoSeat = Locomotive.locoSeat
local EngineerEvent = script.Parent.EngineerEvent
local Driver = LocoSeat.Occupant
local hasWater = script.Parent.EngineerLocal.HasWater
local speed = Locomotive.locoSeat.Velocity.Magnitude
local waterdropping = false

function changeWaterLevel(level, loops, length)
length = length/loops
loops = 1/loops

while true do
wait(length)
Water.Value = Water.Value * (1-loops) + level * loops
if math.floor(Water.Value) == level then
Water.Value = math.floor(Water.Value)
break
end
end
end

changeWaterLevel()

dont worry theres a lot of variables there that just arent used, some from the attempts beforehand and some from others

Well I recreated your scenario and there is no issues with the provided function. There is another issue in your code somewhere.
When you call the function make sure all parameters are filled, like this:
changeWaterLevel(5, 2, 1)

Just use a while loop and a coroutine.
local function waterloss()
while true do
if Water.Value <= 2 then
wait(5) – wait 5 seconds
else
wait(1)
Water.Value = Water.Value - 2
end
end
end

spawn(waterloss)

1 Like

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