How do i automatically set the max stage

i want it to search through a folder and set max stage to the highest number


local player = game.Players.LocalPlayer
local stage = player:WaitForChild("leaderstats"):FindFirstChild("Stage")
local workspace = game.Workspace

local maxStage

for _, stage in workspace.Stages:GetChildren() do
	maxStage = tonumber(stage.Name, maxStage)
end

local function updateProgress()
	local percentage = (stage.Value / maxStage) * 100
	script.Parent.ProgressFrame.Bar:TweenSize(UDim2.new(0, (percentage / 100 * 490), 1, 0), "Out", "Linear", 1)
	script.Parent.ProgressFrame.Percent.Text = string.format("%d%%", percentage)
end

updateProgress()

stage.Changed:Connect(updateProgress)

i know i can use tonumber() but dont know how exactly to use it for this specific use case

1 Like
local maxStage

for _, stage in workspace.Stages:GetChildren() do
    if tonumber(stage.Name) > maxStage then
        maxStage = tonumber(stage.Name)
    end
end

stage.Value = maxStage

You should also make sure you don’t have duplicate variable names (stage in the loop and stage for the leaderstats value)

The second argument in tonumber is the base, see the documentation here: Lua Globals | Documentation - Roblox Creator Hub