I have a system where you can Upgrade an animal’s Value to increase your earnings. I don’t think it is efficient at all, What I have is each Upgrade in a frame where I have to change the values manually
this was also used in one of my previous games and I want this new game to more efficient
This is what one upgrade looks like, I have tried to use Module scripts to make a list of Upgrades and their data Values, and when I try to loop through them It always results in some type of error
This is the script that is inside the Buy Button
--TODO
--[[
Make it compatible with dataStore 2
]]--
local CurrentUpgrade = 0
script.Parent.MouseButton1Click:Connect(function()
local Player = script:FindFirstAncestorOfClass("Player")
local Price = script.Parent.Parent.Price
local MaxUpgrade = script.Parent.Parent.MaxUpgrade
local Button = script.Parent
local Money = Player.leaderstats.Money
local TotalNumber = script.Parent.Parent.TotalNumber
local FarmValue = Player.SemiStats.FarmValue
local ChickenValue = Player.SemiStats.ChickenValue
local LifeTimerearnings = Player.SemiStats.LifeTimeEarnings
local Bar = script.Parent.Parent.ProgressBar.ProgressBar
if CurrentUpgrade < MaxUpgrade.Value then
if Money.Value >= Price.Value then
CurrentUpgrade += 1
Money.Value -= Price.Value
ChickenValue.Value += 10
LifeTimerearnings.Value += Price.Value
Price.Value += 5.5
TotalNumber.Text = CurrentUpgrade
Button.Text = "$ "..Price.Value
Bar.Size = UDim2.new(1/(MaxUpgrade.Value/CurrentUpgrade), 0, 1, 0) -- This Changes The Progress Bar
end
else
Button.Visible = false
end
end)
I’ll be happy to give any missing info
Thanks in advance
Are your animals models or GUIs?
Hello, Sorry I did not get back to you right away, My animals are models, which are located in replicated storage with a number value as it animalValue
Hey but what is the actual issue with your code? Is everything working fine or not? Sorry I just dont have a lot of time to read it, so if everything works okay you are fine I guess.
Everything works but I want to know if there is a more efficient system instead of having to make the upgrades by hand
Ok so heres what I would do (this is just how I would do it; im not saying its the best way):
-- basic variables
local CurrentUpgrade = 0
local Button = script.Parent
local AnimalUpgradeFrame = Button.Parent
-- animal upgrade frame variables
local Price = AnimalUpgradeFrame.Price
local MaxUpgrade = AnimalUpgradeFrame.MaxUpgrade
local TotalNumber = AnimalUpgradeFrame.TotalNumber
Button.MouseButton1Click:Connect(function()
-- get the player
local Player = script:FindFirstAncestorOfClass("Player")
-- get player stats
local Money = Player.leaderstats.Money
local FarmValue = Player.SemiStats.FarmValue
local ChickenValue = Player.SemiStats.ChickenValue
local LifeTimeEarnings = Player.SemiStats.LifeTimeEarnings
local Bar = AnimalUpgradeFrame.ProgressBar.ProgressBar
-- check if the player hasnt reached the upgrade limit yet
if CurrentUpgrade < MaxUpgrade.Value then
-- check if the player has enough money for an upgrade
if Money.Value >= Price.Value then
-- take away player's money
Money.Value -= Price.Value
-- increment current upgrade variable
CurrentUpgrade += 1
-- give animal upgrade to increase production
ChickenValue.Value += 10
-- increase player life time earnings value
LifeTimeEarnings.Value += Price.Value
-- now make the next upgrade cost more money
Price.Value += 5.5
-- show the player his current upgrade value
TotalNumber.Text = CurrentUpgrade
-- show the player next upgrade cost
Button.Text = "$ " .. Price.Value
-- change progress bar
Bar.Size = UDim2.new(1/(MaxUpgrade.Value/CurrentUpgrade), 0, 1, 0) -- This Changes The Progress Bar
end
else
Button.Visible = false
end
end)