Trying to get the percent to load in instantly

When I join the game, i am trying to get the percent to calculate the player’s current stage by the amount of stages in the game. This is my script. If I do need improvements please tell me.

local bar = script.Parent
local stage = game.Players.LocalPlayer:WaitForChild("leaderstats").Stage
local amnt = 50

local per = Instance.new("IntValue")
per.Name = "percent"
per.Parent = script.Parent

script.Parent.Text = "Stage "..stage.Value.."/50".." "..("("..(per.Value).."%"..")")

stage.Changed:Connect(function()
	script.Parent.Text = "Stage "..stage.Value.."/50"..""..("("..(per.Value).."%"..")")
end)
	
per.Changed:Connect(function()
	if per ~= nil then
	per = (stage.Value / amnt) * 100
	script.Parent.Text = "Stage ()"..stage.Value.."/50".." "..("("..(per.Value).."%"..")")	
			
	end
end)

i think theres honestly so many codes in the wrong spot

Scripting support is for code that doesnt work as intended, if your looking for code improvement feedback go to Code review.

I would put this in a function so that you do not have to copy and paste this every time (repeated three times).

per can only be nil if the player gets removed from the game. Instead, you can remove this if-statement and disconnect before the player leaves.

This is possible, but I would personally use a BindableEvent for this. Events allow you to not only keep track of how a stage changes, but also why. For example, a stagePassedBindableEvent also tells you what happend in contrast to an IntValue that only keeps track of what stage you are in.

Maybe you can write some code to procedurally get the amount of stages you have in your game. Otherwise, you might forget about this variable when the amount of stages changes and break your leaderstats.

i get it but i dont really know how to write the code @Brickman808