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

How to make a Client-Sided countdown?

Somehow it doesn't go down.

--variables
local value = script.Parent.Value.Value --the countdown value
local text = script.Parent.Text --this is for the gui, ignore
value = 15 --i will start off with 15
repeat --repeats until the value equals to zero
	wait(1)
	print(value)
	value = value - 1
	text = (value .. " seconds until you can buy another item.") --ignore
until value == 0 --done

1 comment

0

its exploiters heaven to make clienside scripts

1 answer

✓ Accepted answer
  • numbers (-200, 50, 3.141, ...)
  • strings ("hello", 'aaa', 'alligator', ...)
  • nil
  • booleans (true, false)

Are called primites, this means that whenever you assign these values into a variable, they are copied, modifying the new variable will no longer modify the previous variable in which these values were stored.

Value.Value is a number, that means whenever you put it a new variable (you put it into "value") it will create a new number that will be the same number, but no longer related to the previous variable. Whenever you modify "value" variable, you are modifying that one variable, not Value.Value.

Unlike Value.Value, the Value on the left is an Instance, these are not primitives meaning you can put it into variable and it won't get copied, that's what you need:

-- local value = script.Parent.Value.Value <-- no!
local value = script.Parent.Value

Next time you'd like to decrease the value, you have to write value.Value = ... instead.

Same applies to script.Parent.Text, this is string (text), it's primitive and is copied too, changing text variable will only change it, nothing else... and so do the same thing as with value, write script.Parent.Text = ... directly, you can shorten this by creating variable that will refer to script.Parent:

-- local text = script.Parent.Text <-- no!
local label = script.Parent

label.Text = "Some text"

In the end, this is what you get:

--variables
local label = script.Parent

local value = label.Value --the countdown value
value.Value = 15 --i will start off with 15

repeat --repeats until the value equals to zero
    task.wait(1)
    print(value.Value)
    value.Value -= 1
    label.Text = (value.Value .. " seconds until you can buy another item.") --ignore
until value.Value == 0 --done

One thing i can suggest is to use task.wait over wait, there's not much difference, it's overall just more performance, also instead of writing x = x - 1 there is a shortcut x -= 1.

When you get better see "roblox for loops", maybe AlvinBlox. When you get even better see "roblox task.wait delta time".

1 comment

0

Respect for a quality explanation for newbies. I remember that I had to learn such things without anyone's help back in 2015.

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