Why isn't my value getting doubled?
The UpgradeAmount --Value is 2, so it should get doubled when I purchase the upgrade.
How can I fix this?
The area where the value should get doubled lies at line 13
repeat wait() Player = game.Players.LocalPlayer Button = script.Parent Cost = script.Parent.Price.Value Money = Player.leaderstats.Money UpgradeAmount = script.Parent.Parent.UpgradeIncome.UpgradeAmount until Button.MouseButton1Down:connect(function(purchase) if Money.Value > (Cost - 1) then Money.Value = Money.Value - Cost script.Parent.Parent.ClickForMoney.MoneyToGain.Value = script.Parent.Parent.ClickForMoney.MoneyToGain.Value * UpgradeAmount.Value local PurchaseCounter = game.Players:FindFirstChild(Player.Name):FindFirstChild("PurchaseCounterFor"..Player.Name) local TotalPurchases = script.Parent.Parent.Parent.TotalPurchases print( "Cha-Ching! "..script.Parent.Name.." Was Bought By "..Player.Name.."! " ..script.Parent.Price.Value.."$ Has Been Charged From "..Player.Name.. "'s In-Game Money! Enjoy Your "..script.Parent.Name..", "..Player.Name.."!" ) PurchaseCounter.Value = PurchaseCounter.Value + 1 TotalPurchases.Text = "Total Purchases: "..PurchaseCounter.Value local Sound = Instance.new("Sound") --The Cha-Ching Sound Sound.SoundId = "http://www.roblox.com/asset/?id=131886985" Sound.Parent = Player.PlayerGui.SoundFixGui local SoundToggle = game.Players:FindFirstChild(Player.Name):FindFirstChild("SoundOnOffFor"..Player.Name) if SoundToggle.Value then Sound:Play() wait(1) Sound:Remove() else Sound:Stop() wait(1) Sound:Remove() end print("Money Increase Per Click Increased By *"..script.Parent.Parent.UpgradeIncome.UpgradeAmount.Value.." For: "..Player.Name) end wait(0.001) script.Parent:Destroy() print("UpgraderGui#1 Destroyed After Purchase By: "..Player.Name) end)
There are a few things I noticed here that you could clean up. But first, I will address your doubling value problem. In your script, you never actually change the value of UpgradeValue
. In order to change the value of a variable, you must first declare it then multiply itself by two.
UpgradeAmount = UpgradeAmount*2 --multiplies UpgradeAmount by 2
You should also move your variables declared on lines 2-6 into your function so that it doesn't cause too much lag, moving them into the function would cause your variables to change every time the function is called, not every 1/1000 of a second. Just make sure to declare them before you use them.
Another thing I noticed is that on line 9 you used Money.Value > Cost - 1
. Which could translate to money is no less than the cost. For that we would use >= or greater than or equal to. Also, I recommend keeping your conditions in parentheses
if(Money.Value >= Cost)then end
I also noticed how long your hierarchies got. You can use something similar to GCD (Greatest Common Divisor) to rule out things that they have in common, like how script.Parent.Parent
and script.Parent
are used so much. I assume that script.Parent.Parent
is a part, and script.Parent
is the upgrade, so we will define these two variables inside of your script
local upgrade = script.Parent local part = upgrade.Parent
Now just replace your script.Parent.Parent and script.Parents with your upgrade and part variables. Other than that, you should be good to go.
P.S. I'm curious what SoundToggle
is, could you please tell me?
Player = game.Players.LocalPlayer Button = script.Parent Cost = script.Parent.Price.Value Money = Player.leaderstats.Money UpgradeAmount = script.Parent.Parent.UpgradeIncome.UpgradeAmount Button.MouseButton1Down:connect(function(purchase) if Money.Value > (Cost - 1) then Money.Value = Money.Value - Cost script.Parent.Parent.ClickForMoney.MoneyToGain.Value = script.Parent.Parent.ClickForMoney.MoneyToGain.Value * UpgradeAmount.Value local PurchaseCounter = game.Players:FindFirstChild(Player.Name):FindFirstChild("PurchaseCounterFor"..Player.Name) local TotalPurchases = script.Parent.Parent.Parent.TotalPurchases print( "Cha-Ching! "..script.Parent.Name.." Was Bought By "..Player.Name.."! " ..script.Parent.Price.Value.."$ Has Been Charged From "..Player.Name.. "'s In-Game Money! Enjoy Your "..script.Parent.Name..", "..Player.Name.."!" ) PurchaseCounter.Value = PurchaseCounter.Value + 1 TotalPurchases.Text = "Total Purchases: "..PurchaseCounter.Value local Sound = Instance.new("Sound") --The Cha-Ching Sound Sound.SoundId = "http://www.roblox.com/asset/?id=131886985" Sound.Parent = Player.PlayerGui.SoundFixGui local SoundToggle = game.Players:FindFirstChild(Player.Name):FindFirstChild("SoundOnOffFor"..Player.Name) if SoundToggle.Value then Sound:Play() wait(1) Sound:Remove() else Sound:Stop() wait(1) Sound:Remove() end print("Money Increase Per Click Increased By *"..script.Parent.Parent.UpgradeIncome.UpgradeAmount.Value.." For: "..Player.Name) end wait(0.001) script.Parent:Destroy() print("UpgraderGui#1 Destroyed After Purchase By: "..Player.Name) end)
Removed the repeat wait() and until
You can't do
if SoundToggle.Value then Sound:Play() wait(1) Sound:Remove() else Sound:Stop() wait(1) Sound:Remove() end
if the SoundToggle is IntValue
You don't have to do
game.Players:findFirstChild(Player.Name)
when you have the Player variable
There is no point of using
Button.MouseButton1Down:connect(function(purchase)
when you don't need the "purchase" variable MouseButton1Click would be better
I've checked it many times and it works for me.
Here is the whole script (Fixed)
Player = game.Players.LocalPlayer Button = script.Parent Cost = script.Parent.Price.Value Button.MouseButton1Click:connect(function() local UpgradeAmount = script.Parent.Parent.UpgradeIncome.UpgradeAmount local ClickForMoney = script.Parent.Parent.ClickForMoney local Money = Player.leaderstats.Money if Money.Value >= Cost then Money.Value = Money.Value - Cost ClickForMoney.MoneyToGain.Value = ClickForMoney.MoneyToGain.Value * UpgradeAmount.Value local PurchaseCounter = Player:FindFirstChild("PurchaseCounterFor"..Player.Name) local TotalPurchases = script.Parent.Parent.Parent.TotalPurchases print( "Cha-Ching! "..script.Parent.Name.." Was Bought By "..Player.Name.."! " ..script.Parent.Price.Value.."$ Has Been Charged From "..Player.Name.. "'s In-Game Money! Enjoy Your "..script.Parent.Name..", "..Player.Name.."!" ) PurchaseCounter.Value = PurchaseCounter.Value + 1 TotalPurchases.Text = "Total Purchases: "..PurchaseCounter.Value local Sound = Instance.new("Sound") --The Cha-Ching Sound Sound.SoundId = "http://www.roblox.com/asset/?id=131886985" Sound.Parent = Player.PlayerGui.SoundFixGui local SoundToggle = Player:FindFirstChild("SoundOnOffFor"..Player.Name) if SoundToggle.Value == 1 then Sound:Play() wait(1) Sound:Remove() elseif SoundToggle.Value == 0 then Sound:Stop() wait(1) Sound:Remove() end print("Money Increase Per Click Increased By *"..script.Parent.Parent.UpgradeIncome.UpgradeAmount.Value.." For: "..Player.Name) end wait(0.001) script.Parent:Destroy() print("UpgraderGui#1 Destroyed After Purchase By: "..Player.Name) end)
THIS IS THE SOLUTION
I do actually need all of the stuff you say I do not need, Vinteriss. Some of it is being used in other scripts as well.
I do indeed need the SoundToggle section. It's what makes it possible to toggle the purchase-sound.
It was kind of you to try to help me, Vinteriss, but before you answer of these scripting questions, I suggest you should check your facts, and how Lua works.
Oh, and I forgot to mention: I solved it on my own.
I had to make an individual IntValue for each player. Then when the player purchases the upgrade, it gets doubled.
Thank you to everyone who tried to help me.