Unit not uprading to 3 level

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
2 Likes

Looking at your script… Wouldn’t it be currentLevel <= maxUpgradeLevel ?

2 Likes

The issue lies in your conditionals.

if currentLevel < maxUpgradeLevel then

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.

1 Like

ok i will try that and see if it works thank you

3 Likes

So it didnt work and wonder if its a problem the folder its in because both of the upgrades are in the same folder

2 Likes

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.

1 Like

its just says the player has upgraded the unit its just to debug

1 Like

show us tbe folder and the upgrades of that specific unit and why u are useing stringvalue instead of numbervalue

1 Like


Here is where the folders are at and im using a string value because it upgrades by name and it updates by using the stringvalue

1 Like

It never says “unitName … is already at the maximum upgrade level.” ?

1 Like

Is it printing “is already at the maximum upgrade level.”?
Is it an Upgrade, unit:FindFirstChild(“Upgrade”)?

no it never says that because the unit isnt at the maxium level, it only goes to level 2 and then doesnt go to level 3

1 Like

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

1 Like

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)
1 Like

So, did you fix your issue? I was talking about the local script that contains upgradeUnitEvent:fireServer().

1 Like

that script is a server script should it be turned into a local script

No; but can you send the script that contains upgradeUnitEvent:fireServer() its in a module or a local script

This one?

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)


No a local script, that the server who listen for a event but this event is send by the client in a local script…

Oh the module script?

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