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?
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
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.
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
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