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:
Exponential:
please let me know if you have a solution to this problem.
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
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.
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.
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.
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.
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