Value of type nil cannot be converted to a number

It’s a strange warning that appears in the output even though i already set Gold as a NumberValue and defined some values in sessionData[player.UserId]

btw the error appears near the bottom of the script

-- Loads player's stats
local function PlayerAdded(player)
	-- Creates leaderboard and its stats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local money = Instance.new("NumberValue")
	money.Name = "Gold"
	money.Parent = leaderstats
	
	local success = nil
	local playerData = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId) -- Attempting to retrieve player sessionData from Roblox Datastores
		end)
		
		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
		
	until success or attempt == 5
	
	if success then
		print("Data retrieved")
		
		if not playerData then
			print("New player, giving default playerData")
			playerData = {
				["Gold"] = 20,
				["Other"] = 2
			}
		end
		
		sessionData[player.UserId] = playerData 
		for i, v in playerData do
			print(v, playerData, playerData.Gold)
			
		end
	else
		warn("Unable to get sessionData for player ", player.UserId)
		player:Kick("There was a problem when getting your sessionData, try rejoin the game")
	end
	
	money.Value = sessionData[player.UserId].Gold -- Line where error appears
	money.Changed:Connect(function()
		sessionData[player.UserId].Gold = player.leaderstats.Gold.Value
	end)
	
	leaderstats.Parent = player
end

Players.PlayerAdded:Connect(PlayerAdded)

Can someone please help with this problem
Thank you!

3 Likes

When I test this out in studio everything works fine, though it does keep giving my the default player data which could be the cause of no issue, could you describe the situation you are in more thoroughly, as you could be retrieving the data incorrectly.

turns out my player had an existing data with a different name so i had to change it