How can I work around values getting added exponentially?

Hey Roblox devs. Everytime when I try to do a repetitive thing such as giving a coin to a player everytime they press a button, instead of adding the coins in a linear form, it does it exponentially.

To be specific, in my game I’m trying to add a level to a gun everytime a player upgrades it. But, when you upgrade the gun, it upgrades like this: 1, 2, 4, 7, 11, 16, etc.

I’ve tried simply doing Level.Value = Level.Value + 1 each time it gets upgraded, but that still gives the problem. I know it’s not anything wrong with my upgrade script, because it happens all of the time even with other scripts. I’ve also tried doing Level.value += 1 and that still gives the problem. Currently, I’m using a table, like this:

local gunLevelTable = {1}
local gunLevel = game.LocalPlayer.gunLevel

function upgrade()
     Table.insert(gunLevelTable, 1, "1")
     gunLevel.Value = #gunLevelTable

     --[[
     this is supposed to insert a value into the table, which I thought would 
     fix it since insterting a value into a table does not end up exponential, 
     and then finds the number of items in the table, and sets that number
     to the actual gun level.
     --]]
end

(This script is just an example of what I’m doing, it’s not actually what my upgrade script looks like)

If you don’t know what linear and exponential is, here is a visual representation:

Linear:
graph-of-4-5x-3

Exponential:
69883a595f6de177e922ba131fae677fe453760d

please let me know if you have a solution to this problem.

2 Likes

The problem is probably in the context of your script. if you send the other code around it, it might show the issue.

2 Likes

well It’s not, it’s happened to me in many different scripts and I can’t seem to find a solid solution to it. It shouldn’t be in the context of my script, because I only have this code

Table.insert(gunLevelTable, 1, "1")
gunLevel.Value = #gunLevelTable

in one place. I only use gunLevel.Value when the player confirms the upgrade, and when the gui changes. Other than that, I only SET gunLevel.Value once, so it’s weird why it’s happening exponentially. Should I send you my script? It might be hard to understand because It’s very long and complicated.

1 Like

The “upgrade” function is probably being called multiple times, which is why it adds exponentially. When I said show the context I meant show the script that calls “upgrade”, because the problem’s probably there.

You could just be making the same mistake in lots of places, since it doesn’t make sense otherwise.

3 Likes

I don’t have a function… I have a mousebutton1 click detector

so every time the player presses the upgrade button (they can only press it if they have enough to afford the upgrade) it runs the script, and then it sets the gunLevel.Value, which all happens once, so I don’t understand why it’s happening exponentially.

1 Like

As @ScriptingSausage said, since you’ve already tried Level.Value = Level.Value + 1 and got the exact same result (you could also use Level.Value += 1) then it’s the code that is calling this function that is causing the problem.

2 Likes

Try printing something in the function, does it print multiple times when it should only print once? If so, the problem is that the function is being called multiple times for some reason.

(“upgrade” is a function)

meaning put a print here:

function upgrade()
     print(111)
     Table.insert(gunLevelTable, 1, "1")
     gunLevel.Value = #gunLevelTable
3 Likes

I already said I tried using Level.Value += 1.

Sorry, I missed when you posted that.

Just add 1 to a variable and add that variable to the level

local GunLevel = 1
local Add_Value = 0

function Upgrade()
	GunLevel += Add_Value
	Add_Value += 1
	print(GunLevel)
end

Upgrade() -- 1
Upgrade() -- 2
Upgrade() -- 4
Upgrade() -- 7...
1 Like

But they don’t want it added exponentially. It should only be on a 1 per click increase.