Datastore Warning Help

Hi, any idea why it’s warning “value of type nil cannot be converted to a number” for line: player.leaderstats.Rebirths.Value = data[1]

Full code below

local DataStoreService = game:GetService("DataStoreService")
local LeaderboardDS = DataStoreService:GetDataStore("Dev4")

game.Players.PlayerRemoving:Connect(function(player) -- Save data when leving
	local SaveStats = {
		print(player),
		player.leaderstats.Cash.Value,
		player.leaderstats.Rebirths.Value,
	}
	
	local success, fail = pcall(function()
		LeaderboardDS:SetAsync(player.UserId, SaveStats)
	end)

	if success then
		print("Data saved")
	else
		print("Data not saved")
		warn(fail)
	end
	
	
end)

game.Players.PlayerAdded:Connect(function(player) -- Load data function
	local data
	local success, fail = pcall(function()

		data = LeaderboardDS:GetAsync(player.UserId)

	end)

	if success and data then
		player.leaderstats.Cash.Value = data[2]
		player.leaderstats.Rebirths.Value = data[1]
	else
		print("No data")
		player.leaderstats.Cash.Value = 0
		player.leaderstats.Rebirths.Value = 0
	end
end)
1 Like

leaderboards are created after data loaded, i propose you creating table and turn it into leaderstats when player joins the game

Note: you don’t have to set value to 0 if it’s else

I’ll not go into more advanced ways of making data store but consider using mine solutions

It works when loading data for cash, but not rebirths, so I don’t think loading is the issue.

Now i see, you made wrong thing, you index rebirths as nil, because data[1] doesn’t exist at all!

you should write:

player.leaderstats.Cash.Value = data.Cash -- example

The problem is the print inside the table.
this should fix the problem!

local SaveStats = {
		player.leaderstats.Cash.Value,
		player.leaderstats.Rebirths.Value,
	}

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