Value of type nil cannot be converted to a number

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DatastoreService = game:GetService("DataStoreService")
local Data = DatastoreService:GetDataStore("publicDATA")
local sessionData = {}

function PlayerAdded(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local stats = Instance.new("Folder", player)
	stats.Name = "stats"
	
	local prices = Instance.new("Folder", player)
	prices.Name = "Prices"
	
	local TotalCoins = Instance.new("NumberValue", stats)
	TotalCoins.Name = "TotalCoins"
	TotalCoins.Value = 0
	
	local TimePlayed = Instance.new("NumberValue", stats)
	TimePlayed.Name = "TimePlayed"
	TimePlayed.Value = 0
	
	local SessionTimePlayed = Instance.new("NumberValue", stats)
	SessionTimePlayed.Name = "SessionTimePlayed"
	SessionTimePlayed.Value = 0
	
	local CoinsPickUp = Instance.new("NumberValue", stats)
	CoinsPickUp.Name = "CoinsPickUp"
	CoinsPickUp.Value = 0
	
	local coins = Instance.new("NumberValue", leaderstats)
	coins.Name = "Coins"
	coins.Value = 0
	
	local level = Instance.new("NumberValue", leaderstats)
	level.Name = "Level"
	level.Value = 0
	
	local Rebirth = Instance.new("NumberValue", stats)
	Rebirth.Name = "Rebirth"
	Rebirth.Value = 0
	
	local levelUpPrice = Instance.new("NumberValue", prices)
	levelUpPrice.Name = "LevelUpPrice"
	levelUpPrice.Value = 100
	
	local RebirthPrice = Instance.new("NumberValue", prices)
	RebirthPrice.Name = "RebirthPrice"
	RebirthPrice.Value = 5

------------------------------------------------------------------------------------------------------------------------------------------------

	local success, playerData = pcall(function()
		return Data:GetAsync(player.UserId)
	end)

	if success then
		print("Data loaded: " .. player.Name)

		if not playerData then
			print("New player, giving default data")

			playerData = {
				["Coins"] = 0, 
				["TotalCoins"] = 0,
				["CoinsPickUp"] = 0,
				["Level"] = 0,
				["TimePlayed"] = 0,
				["Rebirth"] = 0, -- error happens here
			}
		end

		sessionData[player.UserId] = playerData
	else
		warn("Couldn't load data: " .. player.Name)
		player:Kick("Couldn't load your data, rejoin")
	end

	coins.Value = sessionData[player.UserId].Coins
	TotalCoins.Value = sessionData[player.UserId].TotalCoins
	CoinsPickUp.Value = sessionData[player.UserId].CoinsPickUp
	level.Value = sessionData[player.UserId].Level
	TimePlayed.Value = sessionData[player.UserId].TimePlayed
	Rebirth.Value = sessionData[player.UserId].Rebirth

	coins:GetPropertyChangedSignal("Value"):Connect(function()
		sessionData[player.UserId].Coins = coins.Value
		sessionData[player.UserId].TotalCoins = TotalCoins.Value
		sessionData[player.UserId].CoinsPickUp = CoinsPickUp.Value
		sessionData[player.UserId].Level = level.Value
		sessionData[player.UserId].TimePlayed = TimePlayed.Value
		sessionData[player.UserId].Rebirth = Rebirth.Value
	end)
end

Players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(player)

	if sessionData[player.UserId] then

		local success, errorMsg = pcall(function()
			Data:SetAsync(player.UserId, sessionData[player.UserId])
		end)

		if success then
			print("Data saved: " .. player.Name)
		else
			warn("Can't save: " .. player.Name)
		end
	end
end

Players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end


	for _, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end

game:BindToClose(ServerShutdown)

i think this is happens because i already has saved data before i added rebirth but how can i fix it without reseting data

2 Likes

Its a bug in studio (or new beta feature? Dunno). But it doesnt happen in live servers.

i think this is happened because i already has saved data before i added rebirth
maybe i don’t need to worry and it still saves data
my friend will test this after a while

If you are updated new data, you need migrate default data too.

-----------------------------------------------
-- first make a Default data
local DEFAULT_DATA = {
    ["Coins"] = 0, 
    ["TotalCoins"] = 0,
    ["CoinsPickUp"] = 0,
    ["Level"] = 0,
    ["TimePlayed"] = 0,
    ["Rebirth"] = 0, -- error happens here
}
---------------------------------------------
local success, playerData = pcall(function()
    return Data:GetAsync(player.UserId)
end)

if success then
    print("Data loaded: " .. player.Name)

    if not playerData then
        print("New player, giving default data")

        playerData = {
            ["Coins"] = 0, 
            ["TotalCoins"] = 0,
            ["CoinsPickUp"] = 0,
            ["Level"] = 0,
            ["TimePlayed"] = 0,
            ["Rebirth"] = 0, -- error happens here
        }
    end
    ------------------------------------------------------------
    -- second migrate data, only for value
    for k, v in DEFAULT_DATA do
        assert(typeof(v) == "table", `You must do something to migrate table data with key {k}!`)
        playerData[k] = playerData[k] or v
    end

    sessionData[player.UserId] = playerData
else
    warn("Couldn't load data: " .. player.Name)
    player:Kick("Couldn't load your data, rejoin")
end

Full source

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DatastoreService = game:GetService("DataStoreService")
local Data = DatastoreService:GetDataStore("publicDATA")
local sessionData = {}
-- first make a Default data
local DEFAULT_DATA = {
    ["Coins"] = 0, 
    ["TotalCoins"] = 0,
    ["CoinsPickUp"] = 0,
    ["Level"] = 0,
    ["TimePlayed"] = 0,
    ["Rebirth"] = 0,
}

function PlayerAdded(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local stats = Instance.new("Folder", player)
	stats.Name = "stats"
	
	local prices = Instance.new("Folder", player)
	prices.Name = "Prices"
	
	local TotalCoins = Instance.new("NumberValue", stats)
	TotalCoins.Name = "TotalCoins"
	TotalCoins.Value = 0
	
	local TimePlayed = Instance.new("NumberValue", stats)
	TimePlayed.Name = "TimePlayed"
	TimePlayed.Value = 0
	
	local SessionTimePlayed = Instance.new("NumberValue", stats)
	SessionTimePlayed.Name = "SessionTimePlayed"
	SessionTimePlayed.Value = 0
	
	local CoinsPickUp = Instance.new("NumberValue", stats)
	CoinsPickUp.Name = "CoinsPickUp"
	CoinsPickUp.Value = 0
	
	local coins = Instance.new("NumberValue", leaderstats)
	coins.Name = "Coins"
	coins.Value = 0
	
	local level = Instance.new("NumberValue", leaderstats)
	level.Name = "Level"
	level.Value = 0
	
	local Rebirth = Instance.new("NumberValue", stats)
	Rebirth.Name = "Rebirth"
	Rebirth.Value = 0
	
	local levelUpPrice = Instance.new("NumberValue", prices)
	levelUpPrice.Name = "LevelUpPrice"
	levelUpPrice.Value = 100
	
	local RebirthPrice = Instance.new("NumberValue", prices)
	RebirthPrice.Name = "RebirthPrice"
	RebirthPrice.Value = 5

------------------------------------------------------------------------------------------------------------------------------------------------

	local success, playerData = pcall(function()
		return Data:GetAsync(player.UserId)
	end)

	local success, playerData = pcall(function()
        return Data:GetAsync(player.UserId)
    end)
    
    if success then
        print("Data loaded: " .. player.Name)
    
        if not playerData then
            print("New player, giving default data")
    
            playerData = {
                ["Coins"] = 0, 
                ["TotalCoins"] = 0,
                ["CoinsPickUp"] = 0,
                ["Level"] = 0,
                ["TimePlayed"] = 0,
                ["Rebirth"] = 0, -- error happens here
            }
        end
        ------------------------------------------------------------
        -- second migrate data, only for value
        for k, v in DEFAULT_DATA do
            assert(typeof(v) == "table", `You must do something to migrate table data with key {k}!`)
            playerData[k] = playerData[k] or v
        end
    
        sessionData[player.UserId] = playerData
    else
        warn("Couldn't load data: " .. player.Name)
        player:Kick("Couldn't load your data, rejoin")
    end

	coins.Value = sessionData[player.UserId].Coins
	TotalCoins.Value = sessionData[player.UserId].TotalCoins
	CoinsPickUp.Value = sessionData[player.UserId].CoinsPickUp
	level.Value = sessionData[player.UserId].Level
	TimePlayed.Value = sessionData[player.UserId].TimePlayed
	Rebirth.Value = sessionData[player.UserId].Rebirth

	coins:GetPropertyChangedSignal("Value"):Connect(function()
		sessionData[player.UserId].Coins = coins.Value
		sessionData[player.UserId].TotalCoins = TotalCoins.Value
		sessionData[player.UserId].CoinsPickUp = CoinsPickUp.Value
		sessionData[player.UserId].Level = level.Value
		sessionData[player.UserId].TimePlayed = TimePlayed.Value
		sessionData[player.UserId].Rebirth = Rebirth.Value
	end)
end

Players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(player)

	if sessionData[player.UserId] then

		local success, errorMsg = pcall(function()
			Data:SetAsync(player.UserId, sessionData[player.UserId])
		end)

		if success then
			print("Data saved: " .. player.Name)
		else
			warn("Can't save: " .. player.Name)
		end
	end
end

Players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end


	for _, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end

game:BindToClose(ServerShutdown)
  • So if you have tabular data, you have to recursively migrate the data across that table
1 Like

ok sorry guys my friend tested and everything working fine

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.