i am making a tower defense game and i just finished the tower upgrade system, it works when there is only one tower, but when there is two towers, it would only upgrade one of them, so even if i upgrade the other one, the first one will get upgraded instead of the second one, i know its complicated, but i just cant fix it, btw, both are server side and they use the same module script, here is what their files look like:
here is their scripts:
modulescript:
local sys = {}
sys.__index = sys
local upgradeFrame = nil
local info
local tower
local TowerFolders
function sys.Set(x)
upgradeFrame = x
info = upgradeFrame.Model.Value.Configuration
tower = upgradeFrame.Model.Value
TowerFolders = tower.Upgrades
print("set!")
print(TowerFolders.Name)
end
function sys.EditValues(x)
local info2 = upgradeFrame.Folder
local TowersInfo = x
print(TowerFolders.Parent.Name)
upgradeFrame.UpgradeInfo.Text = tostring(x.Parent.Text.Desc.Value)
upgradeFrame.UpgradeName.Text = tostring(x.Parent.Text.name.Value)
info.Damage.Value = TowersInfo.Damage.Value
info.Range.Value = TowersInfo.Range.Value
info.StealthDetection.Value = TowersInfo.StealthDetection.Value
info.Speed.Value = TowersInfo.Speed.Value
info2.Damage.Text = "Damage: "..info.Damage.Value
info2.Range.Text = "Range: "..info.Range.Value
info2.SD.Text = "Stealth: "..tostring(info.StealthDetection.Value)
info2.Speed.Text = "Speed: "..info.Speed.Value
end
function sys.Upgrade()
print(TowerFolders)
local value = upgradeFrame.Model.Value.Configuration.num.Value
for _, v in pairs(TowerFolders["Tower"..value]:GetDescendants()) do
pcall(function()
v.Transparency = 1
end)
end
for _, v in pairs(TowerFolders["Tower"..value+1]:GetDescendants()) do
pcall(function()
if v.Name == "HumanoidRootPart" then
v.Transparency = 1
else
v.Transparency = 0
end
end)
end
sys.EditValues(TowerFolders["Tower"..value+1].stats)
upgradeFrame.Model.Value.Configuration.num.Value += 1
print(value)
end
return sys
UpgradeGui:
script.Parent.MouseClick:Connect(function(x)
wait(0.1)
print(script.Parent.Parent.Parent:GetChildren())
local gui = script.Parent.Parent.Parent.Info2
if x.PlayerGui.ScreenGui:FindFirstChild("Info2") then
x.PlayerGui.ScreenGui.Info2.Parent = x.PlayerGui.ScreenGui.Info2.Model.Value
end
gui.Parent = x.PlayerGui.ScreenGui
end)
upgrade button serverscript (inside “Upgrade” button):
local upgrades = require(game.ServerScriptService.Upgrades)
local frame = script.Parent.Parent
local deb = true
local del = 0.5
upgrades.Set(frame)
upgrades.EditValues(frame.Model.Value.Upgrades["Tower1"].stats)
script.Parent.MouseButton1Down:Connect(function()
if deb == true then
deb = false
if frame.Model.Value.Configuration.num.Value < #frame.Model.Value.Upgrades:GetChildren() then
upgrades.Upgrade()
end
end
wait(del)
deb = true
end)
if you have any questions about the code, or simply need more info, feel free to ask.
Thanks for reading.