Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i make a "fuel" and "refill" system?

In short i have a part with a number value named "Time" and has its number set to 100 and a surface gui with a text label inside of the surface gui. There is a script inside the text label that is supposed to tell the text label to set its label as the number value minus 1 every second to make it count down from 100. There is a button beside the part that adds 10 points to the number value on click. The maximum is set as 100 and if the player tries to spam the button, it will be capped at 100 and wont go any higher. My problem is when i try to set a minimum number value at 0 (so the numbers will stay at 0 and wont go any lower) it wont work. It looks fairly confusing to begin with as I've not done something like this before (or have really ever used values in a script). It's supposed to be a fuel bar and the button refills it just for context. Here's the script starting from the button to the main countdown script.

script.Parent.ClickDetector.MouseClick:Connect(function()
	if game.Workspace.CountdownPart.Time.Value <= 100 then
	game.Workspace.CountdownPart.Time.Value = game.Workspace.CountdownPart.Time.Value + 10
	end
end)

And here's the countdown script.

local countdown = script.Parent.Parent.Parent.Time

script.Parent.Text = countdown.Value

for i = 1,countdown.Value do
 wait(3)
 countdown.Value = countdown.Value - 10
 script.Parent.Text = countdown.Value
end

while true do
if countdown.Value >= 100 then
	countdown.Value = 100
end
if countdown.Value <= 0 then
	countdown.Value = 0
end
end

1 answer

✓ Accepted answer

You can't have 2 loops running at the same time without a new thread being created. However, you don't need to get into making new threads as that would over-complicate your system, so what you need to do is use event listeners.

One way you could do it is listen for the changing of the countdown value:

local countdown = script.Parent.Parent.Parent.Time

script.Parent.Text = countdown.Value


for i = countdown.Value,1,-1 do --the countdown.Value, until 1, subtract 1
 wait(1) --waits 1 second
 countdown.Value = i
 script.Parent.Text = countdown.Value
end

countdown.Changed:Connect(function(property)
    if property == countdown.Value then
        if countdown.Value > 99 then
            countdown.Value = 100
        end
        elseif countdown.Value < 1 then
            countdown.Value = 0
        end
    end
end)

Also a sidenote: in Roblox lua specifically, it's a bad practice to use while true loops. In other languages, that's completely fine, but Roblox lua has so many better ways to handle loops that you should just stick to those methods.

And let me know if this doesn't work because I'm doing it off the top of my head without testing as I am answering via mobile right now.

4 comments

0

Ah okay this is a fresh perspective! had no idea this thing existed. Ill test it out soon and tell you if it works correctly.

0

Tried it and it posted this error: Expected ')' (to close '(' at line 15), got 'end'. It seems like your going into the right direction though. Can you correct this soon? I'm not too sure whats causing it.

0

I was able to remove the error by removing the third end) and adding the ")" to the second end. Apparently this caused the script to repeat the problem and now the numbers still lower into the negatives but the numbers dont go past 100 as usual.

0

Hold on a second, why do you have a fuel and refill system in your question title, but then talk about a timer in the details? What is your whole system trying to do?

This discussion is preserved as it was. New answers, comments, and votes are closed.