Problems with IntValue Help

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.

1 Like

Here a local script what fire server:

 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
1 Like

Don’t use an IntValue! Use a NumberValue.

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.

1 Like

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)

Yes I tried.:sob: But it didnt work

Check video:
Uploading: bandicam 2024-06-05 15-19-15-513.mp4…

I might’ve misunderstood. I’m having trouble figuring out exactly what you’re trying to accomplish here. I saw the

and assumed you accidentally were using IntValue instead of NumValue. My apologies!

Could you please explain further what exactly you are trying to accomplish? For Example:

What are the numbers that are being inputted? What is it doing that you expect it to go from 1 to 2 and back to 1 instead of going up?

1 Like

1 Like

YES, VIDEO UPLOADED!sdfcvbnmghj

Hey proxiom!
You’re not updating the “boughtLevels” variable in the code block you provided did you forget a

boughtLevels += 1

somewhere?

Sorry I got confused with your issue as much as you are :sweat_smile:

Look video
ddddddddddcccccccccccd

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.

1 Like

The problem is that I have a lot of such buttons…

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.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.