Hey so i made this script to upgrade my units when the upgrade button is clicked but when i try and upgrade it to the third level it doesn’t work. The upgrades are in a folder and its just the third one that doesnt work does anyone know why
Here’s the script
local replicatedStorage = game:GetService("ReplicatedStorage")
local units = replicatedStorage:WaitForChild("Units")
local upgrades = units:WaitForChild("Upgrades")
local upgradeUnitEvent = replicatedStorage:WaitForChild("UpgradeUnit")
local maxUpgradeLevel = 3
local function upgradeUnit(player, unitName)
local unit = upgrades:FindFirstChild(unitName)
if unit then
local upgradeValue = unit:FindFirstChild("Upgrade")
if upgradeValue and upgradeValue:IsA("StringValue") then
local currentLevel = tonumber(upgradeValue.Value) or 0
if currentLevel < maxUpgradeLevel then
local nextLevel = currentLevel + 1
upgradeValue.Value = tostring(nextLevel)
print(player.Name .. " upgraded " .. unitName .. " to level " .. nextLevel)
else
print(unitName .. " is already at the maximum upgrade level.")
end
end
end
end
upgradeUnitEvent.OnServerEvent:Connect(upgradeUnit)
type or paste code here
This code doesn’t run when the currentLevel is 3, because it’s only checking to see if currentLevel is LESS than 3, not Less than or Equal to, which is what the <= condition does.
So, your code should look like this.
if currentLevel <= maxUpgradeLevel then
Just a small tweak, but that should fix your code! Let me know if it works.
If I can ask, what’s printed when it checks for currentLevel?
I see you have two prints, print(player.Name .. " upgraded " .. unitName .. " to level " .. nextLevel) & print(unitName .. " is already at the maximum upgrade level.").
I was just curious as to if any of them print, or if nothing at all.
Ok so problem is from the local script can you send it?
Maybe you did the same error in the local script?
This one: if currentLevel < maxUpgradeLevel then
And you need to change it to: if currentLevel <= maxUpgradeLevel then
i fixed that already i do have a module script that is for the units and that contributes to the local script
function units.Spawn(player, name, cframe, previous)
local allowUnitToSpawn = units.CheckSpawn(player, name)
local configuration = units.Configuration
if allowUnitToSpawn then
local newUnit
if previous then
previous:Destroy()
newUnit = replicatedStorage.Units.Upgrades[name]:Clone()
else
newUnit = replicatedStorage.Units[name]:Clone()
end
newUnit.HumanoidRootPart.CFrame = cframe
newUnit.Parent = workspace.Units
newUnit.HumanoidRootPart:SetNetworkOwner(nil)
local replicatedStorage = game:GetService("ReplicatedStorage")
local units = replicatedStorage:WaitForChild("Units")
local upgrades = units:WaitForChild("Upgrades")
local upgradeUnitEvent = replicatedStorage:WaitForChild("UpgradeUnit")
local maxUpgradeLevel = 3
local function upgradeUnit(player, unitName)
local unit = upgrades:FindFirstChild(unitName)
if unit then
local upgradeValue = unit:FindFirstChild("Upgrade")
if upgradeValue and upgradeValue:IsA("StringValue") then
local currentLevel = tonumber(upgradeValue.Value) or 0
if currentLevel <= maxUpgradeLevel then
local nextLevel = currentLevel + 1
upgradeValue.Value = tostring(nextLevel)
print(player.Name .. " upgraded " .. unitName .. " to level " .. nextLevel)
else
print(unitName .. " is already at the maximum upgrade level.")
end
end
end
end
upgradeUnitEvent.OnServerEvent:Connect(upgradeUnit)
local physicsService = game:GetService("PhysicsService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local eventsFolder = replicatedStorage:WaitForChild("Events")
local animateUnit = eventsFolder:WaitForChild("AnimateUnit")
local functionsFolder = replicatedStorage:WaitForChild("Functions")
local placeUnitFunction = functionsFolder:WaitForChild("SpawnUnit")
local requestFunction = functionsFolder:WaitForChild("RequestingUnit")
local maxUnits = 16
local units = {}
function findNearestTarget(newUnit, range)
local nearestTarget = nil
for i, target in ipairs(workspace.FoodEnemys:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - newUnit.HumanoidRootPart.Position).Magnitude
if distance < range then
nearestTarget = target
range = distance
end
end
return nearestTarget
end
function units.Attack(newUnit, player)
local configuration = newUnit.Configuration
local target = findNearestTarget(newUnit, configuration.Range.Value)
if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
local targetCFrame = CFrame.lookAt(newUnit.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newUnit.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
animateUnit:FireAllClients(newUnit, "Attack")
target.Humanoid:TakeDamage(configuration.Damage.Value)
if target.Humanoid.Health <= 0 then
player.leaderstats.Coins.Value += target.Humanoid.MaxHealth
end
task.wait(configuration.Cooldown.Value)
end
task.wait(0.1)
if newUnit and newUnit.Parent then
units.Attack(newUnit, player)
end
end
function units.Spawn(player, name, cframe, previous)
local allowUnitToSpawn = units.CheckSpawn(player, name)
local configuration = units.Configuration
if allowUnitToSpawn then
local newUnit
if previous then
previous:Destroy()
newUnit = replicatedStorage.Units.Upgrades[name]:Clone()
else
newUnit = replicatedStorage.Units[name]:Clone()
end
newUnit.HumanoidRootPart.CFrame = cframe
newUnit.Parent = workspace.Units
newUnit.HumanoidRootPart:SetNetworkOwner(nil)
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
bodyGyro.D = 0
bodyGyro.CFrame = newUnit.HumanoidRootPart.CFrame
bodyGyro.Parent = newUnit.HumanoidRootPart
for i, object in pairs(newUnit:GetDescendants()) do
if object:IsA("BasePart") then
object.CollisionGroup = "Units"
end
end
player.leaderstats.Coins.Value -= newUnit.Configuration.Price.Value
player.PlacedUnits.Value += 1
coroutine.wrap(units.Attack)(newUnit, player)
return newUnit
else
warn("Unit not found:", name)
return false
end
end