While making a progress bar, progress value gives nan number

  1. What do you want to achieve? Keep it simple and clear!
    So currently i’m making an progress bar for my FPS game like in arsenal, for some reason while i’m making this line
local Progress = PlrExp.Value / RequiredExp.Value
print(Progress)

I get nan print and in bar properties it’s inf size, why this happen? PlrExp is 400, RequiredExp is 500.
2. What is the issue? Include screenshots / videos if possible!
Described above
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I was looking video on youtube and i’m doing everything right.

My code:

	local Players = game:GetService("Players")
	local Player = Players.LocalPlayer
	local StatsFolder = Player.StatsFolder
	local RequiredExp = StatsFolder.RequiredExp
	local PlrExp = StatsFolder.PlrExp
	local Bar = script.Parent
	local Progress = PlrExp.Value / RequiredExp.Value
	print(Progress)
	Bar:TweenSize(UDim2.new(Progress,0,1,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quad, 0.5, false)
    PlrExp.Changed:Connect(function()
	    Progress = PlrExp.Value / RequiredExp.Value
		Bar:TweenSize(UDim2.new(Progress,0,1,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quad, 0.5, false)	
	end)

nan only appears when you divide 0 by 0. So, maybe the values aren’t getting updated before you perform the operation?

Hm that could happen, because i use datastore script, which could happen after bar, here is it

local playersService = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("DataStore")
local protectedCall = pcall

local function onPlayerJoined(player)
	
	local StatsFolder = Instance.new("Folder")
	StatsFolder.Parent = player
	StatsFolder.Name = "StatsFolder"
	
	local PlrExp = Instance.new("NumberValue")
	PlrExp.Parent = StatsFolder
	PlrExp.Name = "PlrExp"
	
	local PlrLevel = Instance.new("NumberValue")
	PlrLevel.Parent = StatsFolder
	PlrLevel.Name = "PlrLevel"
	
	local RequiredExp = Instance.new("NumberValue")
	RequiredExp.Parent = StatsFolder
	RequiredExp.Name = "RequiredExp"
	
	local success, result = protectedCall(function()
		return dataStore:GetAsync("Data_"..player.UserId)
	end)

	if success then
		if result then
			if type(result) == "table" then
				PlrExp.Value = result[1] or 0
				PlrLevel.Value = result[2] or 1
				RequiredExp.Value = result[3] or 500
 		end
		end
	else
		warn(result)
	end
end

local function onPlayerLeft(player)
	local success, result = protectedCall(function()
		return dataStore:SetAsync("Data_"..player.UserId, {player.StatsFolder.PlrExp.Value,player.StatsFolder.PlrLevel.Value,player.StatsFolder.RequiredExp.Value
		})
	end)

	if success then
		print(result)
		print("Everything success")
	else
		warn(result)
		warn("everything wrong")
	end
end

local function onServerClosed()
	for _, player in ipairs(playersService:GetPlayers()) do
		local success, result = protectedCall(function()
			return dataStore:SetAsync("Data_"..player.UserId, {player.StatsFolder.PlrExp.Value,player.StatsFolder.PlrLevel.Value,player.StatsFolder.RequiredExp.Value
			})
		end)

		if success then
			print(result)
		else
			warn(result)
		end
	end
end

playersService.PlayerAdded:Connect(onPlayerJoined)
playersService.PlayerRemoving:Connect(onPlayerLeft)
game:BindToClose(onServerClosed) 

Where is the LocalScript located?

Here:
image

Can you try printing the two values before doing the operation? See if it prints 0 for both of them.

Hmm here
image
yeah it 0 0 exactly, i will just add wait 1-2 secs
EDIT: Player anyways will load atleast 3 seconds, so it will happen a lot faster than player join

Yeah then that’s the problem. A quick solution would be to wait until the RequiredExp value is not equal to 0.

local RunService = game:GetService("RunService");
repeat
   RunService.RenderStepped:Wait()
until RequiredExp.Value > 0

But anyways PlayerExp don’t load same, so it will have same result

No, if both are 0 it will give out nan, 0 / 500 or whatever is a possible operation so it would return 0 not nan.

But anyways i had 400 exp at start as i mentioned, and exp saves