Hello, i have problem again with my Tower Upgrade script. so, I got this issues when I trying to upgrade my Support Tower. this is my upgrade script:
EventFolder:WaitForChild("UpgradeEvent").OnServerEvent:Connect(function(Player, Tower)
if Player and Tower then
if TowerModule.Tower[Tower.Name] then
if Tower.Level.Value < 5 then
if Player.Money.Value > TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost then
Player.Money.Value -= TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost
for i, upgrdvalue in pairs(Tower:GetChildren()) do
if upgrdvalue:IsA("NumberValue") then
if TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name] then
upgrdvalue.Value = TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name]
else
warn("Failed to update "..upgrdvalue.Name.." Value because this value is missing or not found; nil")
end
end
end
if Tower:FindFirstChild("Type").Value == "Support" then
wait(.1)
Tower.TowerControl.Disabled = true
wait()
Tower.TowerControl.Disabled = false
end
end
end
end
end
end)
I use ModuleScript to data my tower stats. this is the module script:
the issues is: Somehow when I trying to max my Support tower it says the “Range Buff” is nil. even tho on the data/module the RangeBuff is exist. this is picture the error that I get:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EventFolder = ReplicatedStorage:WaitForChild("EventFolder")
local TowerFolder = ReplicatedStorage:WaitForChild("TowerFolder")
local ServerScriptService = game:GetService("ServerScriptService")
local ModuleScript = ServerScriptService:WaitForChild("ModuleScript")
local TowerModule = require(ModuleScript:WaitForChild("TowerModule"))
EventFolder:WaitForChild("PlaceEvent").OnServerEvent:Connect(function(Player, TowerPosition, Tower)
if Player and Tower then
local TowerClone = TowerFolder:WaitForChild(Tower):Clone()
TowerClone.Parent = workspace:WaitForChild("Tower")
for i, upgrdvalue in pairs(TowerClone:GetChildren()) do
if upgrdvalue:IsA("NumberValue") then
upgrdvalue.Value = TowerModule.Tower[TowerClone.Name][1][upgrdvalue.Name]
end
end
TowerClone.Owner.Value = Player.Name
TowerClone.Level.Value = 1
if TowerClone.PrimaryPart then
TowerClone:SetPrimaryPartCFrame(CFrame.new(TowerPosition))
end
TowerClone:WaitForChild("TowerControl").Disabled = false
warn(TowerClone.Name.." Placed by "..Player.Name)
end
end)
EventFolder:WaitForChild("UpgradeEvent").OnServerEvent:Connect(function(Player, Tower)
if Player and Tower then
if TowerModule.Tower[Tower.Name] then
if Tower.Level.Value < 5 then
if Player.Money.Value > TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost then
Player.Money.Value -= TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost
for i, upgrdvalue in pairs(Tower:GetChildren()) do
if upgrdvalue:IsA("NumberValue") then
upgrdvalue.Value = TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name] -- this is line 36
end
end
if Tower:FindFirstChild("Type").Value == "Support" then
wait(.1)
Tower.TowerControl.Disabled = true
wait()
Tower.TowerControl.Disabled = false
end
end
end
end
end
end)
I just fixed the issues with removing “level” on the Tower Data Module. so this happen because when the scripts check the tower stats data, the level is updated first before the RangeBuff that makes the scripts check level 6(which is not on the data) instead and makes RangeBuff are nil or not found. so I change the upgrade script to be like this:
EventFolder:WaitForChild("UpgradeEvent").OnServerEvent:Connect(function(Player, Tower)
if Player and Tower then
if TowerModule.Tower[Tower.Name] then
if Tower.Level.Value < 5 then
if Player.Money.Value > TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost then
Player.Money.Value -= TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost
for i, upgrdvalue in pairs(Tower:GetChildren()) do
if upgrdvalue:IsA("NumberValue") then
if upgrdvalue.Name ~= "Level" then -- will ignore values named "Level" because "Level" is not on the Tower Data Module
upgrdvalue.Value = TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name]
print(upgrdvalue.Name, " Has been updated to; "..TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name])
end
end
end
Tower.Level.Value += 1 -- Instead I will increase the level value after the other stats updated.
if Tower:FindFirstChild("Type").Value == "Support" then
wait(.1)
Tower.TowerControl.Disabled = true
wait()
Tower.TowerControl.Disabled = false
end
end
else
warn("Tower maxxed out.")
end
end
end
end)
Thank you for helping me and giving me ideas to fix it.^^