How do I make a custom counter?

So I’m trying to create a custom counter but it won’t work, this is my code:

local counter = counter + amount or 1

“amount” is supposed to be a thing that keep counts upwards and “counter” is storing the amount of times the “amount” thing has counted for but it won’t work because it always underlines the second time I called counter and it says " Unknown global “counter” " so it counts “counter” as nil but I have declared counter??? and if I try to test the code it will say “attempt to perform arithmetic (add) on nil and number” even thought I already declared “counter” as a local variable as I have said.

Basically the variable is still in the process of being declared, so you can’t use it yet. What are you trying to do? I think you should provide a bit more code so we can see what you want to do

1 Like

I’m just trying to create a simple counter that stores the amount of times “amount” has counted for as a variable but when I try to update “counter” it always underlines it.

You could start counter at zero.

local counter = 0
counter = counter + (amount or 1) -- parentheses are necessary because + precedes or
1 Like