local boughtLevels = 0
game.ReplicatedStorage.AAA.OnServerEvent:Connect(function(plr,buyLvlPrice,giveMoneyInHour,levelSell,money,buyLvl)
if plr:WaitForChild("Money").Value >= buyLvlPrice.Value then
print(boughtLevels)
plr.Money.Value -= buyLvlPrice.Value
print(boughtLevels)
plr.MoneyInHour.Value += boughtLevels * giveMoneyInHour.Value
plr.PlayerGui.Game.Main.MoneyInHour.Money.Text = tostring(plr.MoneyInHour.Value)
plr.PlayerGui.Game.Profit.MoneyInHour.Money.Text = tostring(plr.MoneyInHour.Value)
local function updateBuyButton()
buyLvl.Text = "lvl " .. boughtLevels
levelSell.Text = buyLvlPrice.Value
money.Text = boughtLevels * giveMoneyInHour.Value
end
buyLvlPrice.Value *= 1.2
updateBuyButton()
else
task.spawn(function()
plr.PlayerGui["No Money"].Enabled = true
wait(0.5)
plr.PlayerGui["No Money"].Enabled = false
end)
end
end)
I have quite a lot of such buttons. Suppose I pressed button No. 1 - Everything works, boughtLevels became equal to 1, pressed boughtLevels for the second time began to equal two. I expect that when pressing button #2, boughtLevels will also become 1. However, it starts to equal three! And most importantly, I understand that the mistake is that I declare a variable for the entire script. BUT. I do not know how to fix it. Help if you know how. Feel free to ask questions.
for _, button in pairs(script.Parent:GetChildren()) do
if button:IsA("TextButton") then
for _, frame in pairs(button:GetChildren()) do
if frame:IsA("Frame") then
local buyLvlPrice = frame:FindFirstChild("BuyLvlPrice")
local giveMoneyInHour = frame:FindFirstChild("GiveMoneyInHour")
local levelSell = frame:FindFirstChild("LevelSell")
local money = frame:FindFirstChild("Money")
local buyLvl = frame:FindFirstChild("BuyLvl")
buyLvl.MouseButton1Click:Connect(function()
game.ReplicatedStorage.AAA:FireServer(buyLvlPrice,giveMoneyInHour,levelSell,money,buyLvl)
end)
end
end
end
end
INTEGER Values only allow numbers that are integers. An integer is a whole number. What this means is, an IntegerValue rounds whatever you input to the nearest Integer. For example, if you input 2.8, it will round to 3.
A number value does not do this, and will keep whatever number you input.
Not a scripter, but I will say it seems your issue might relate to the variable adding to itself which you seemingly don’t want. “boughtLevels * giveMoneyInHour” is multiplying the variable and then adding to the original variable, so my guess is that the issue stems from here. Have you tried using a second variable?
(Pardon if I’m way off, just trying to think of some ideas lol)
I see, well since you used IntValues a lot you could also add a “BoughtLevels” IntValue to each one of the buttons
local function updateBuyButton()
buyLvl.Text = "lvl " .. BoughtLevels.Value
levelSell.Text = buyLvlPrice.Value
money.Text = BoughtLevels.Value * giveMoneyInHour.Value
end
Since as you’ve pointed out the script shares a singular “boughtLevels” value for every single button, you could just use another IntValue for each button to know their Levels individually.
You’re going to need to put the values into each button if you want each one to be different. You can’t use one value and expect it to know which button you’re talking about.