Why is this happening with my data stores?

So basically, I don’t know why but whenever I try to load data for the player on a datastore, an error shows up in the output box that says “value of type nil cannot be converted to a number”.

And I’m so confused because the exact same script works in a different project I have.

My code:

local dataStoreSErvice = game:GetService("DataStoreService")
local mainDataStore = dataStoreSErvice:GetDataStore("MainStore")
local http = game:GetService("HttpService")

local defaultData = {
	["Points"] = 0,
	["Level"] = 1,
}

game:GetService("Players").PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"

	local Points = Instance.new("IntValue", folder)
	Points.Name = "Points"

	local Level = Instance.new("IntValue", folder)
	Level.Name = "Level"

	local data

	local success, err = pcall(function()
		local playerdata = mainDataStore:GetAsync(player.UserId)
		if playerdata then
			playerdata = http:JSONDecode(playerdata)
			data = playerdata
		else
			data = defaultData
		end
	end)

	if err then
		data = defaultData
	end
	Points.Value = data["Points"]
	Level.Value = data["Level"]
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local plrData = {
		["Level"] = player.leaderstats.Level.Value,
		["Points"] = player.leaderstats.Points.Value
	}

	local success,err = pcall(function()
		local data = http:JSONEncode(plrData)

		mainDataStore:SetAsync(player.UserId, data)
	end)

	if err then
		warn(err)
	end
end)

Help appriciated

1 Like

Could you show the error message, so we can see on which line it happens?

But even without it, I am pretty sure the issue is that, since this is a new project, the datastore is currently empty (or one of the fields, data["Points"] or data["Level"] is nil because it hasn’t been set yet)

Since you do not have a default value, in case the player is new, (or if the player is an older player and you added a new field to the datastore), it will try to set Points.Value to nil and Level.Value to nil as well

Also, be careful, if loading fails, saving will happen anyway, but with the default values of your int values. Player data can be completely wiped at random.
I’ve seen this issue very often. I will refer you to this thread in which I talk about that kind of stuff, plus avoiding race conditions

Well, your script logic looks fine. I’d guess the issue is the existing saved data in that place using a different format than what this script expects. Clearing the DataStore or adding a type check could resolves this if that is the case.. If type is string then decode. If type is table then use it directly..

The error happens on lines 35 and 36

1 Like

Oh right I did not notice the defaultData, the logic does indeed seem sound, unless the player only has 1 of the two fields (or an empty table saved), which would prevent defaultData from being used

I would structure it like this instead

Points.Value = data["Points"] or defaultData["Points"]
Level.Value = data["Level"] or defaultData["Level"]

The issue of data being wiped is very much still an issue though

1 Like

Oh that’s odd, it’s a warning rather than an error, and two different lines? Could you specify which line is line 35 and 36 in the code you’ve shared? The warning leads me to believe the error is coming from the PlayerRemoving function (as it’s the only one with a warn), but it makes no sense for the error to be on two seperate lines

This is line 35

and this is line 36

Ok, I had to do a test because InValues throwing a warning instead of an error is really odd, but I guess I’ve learnt something new (although I’m honestly wondering if that’s a bug)

My guess as to why your original code wasn’t working would be that you’ve set your data in the datastore to an empty table, which would prevent the defaultTable from being used instead. My reply you’ve marked as a solution fixes that issue, and I assume it worked, since you’ve marked it as a solution? With that solution, you can also do this inside the pcall, which simplifies things a little

local playerdata = mainDataStore:GetAsync(player.UserId)
data = playerdata or {}

The empty table will receive the default values from the solution you’ve implemented

I will also add that you don’t need to jsonEncode and jsonDecode tables before saving them to the datastore, roblox handles that for you

And you should really look into that data wiping issue I’ve mentioned. If your game gets popular, you’ll run into a handful of players completely losing their data every so often. If it’s just a project that wont get published, then I guess it’s not too important, but it’s still good to know

1 Like

On a side note OP since it seems this has been solved. If the player’s data fails to load that is an issue. A simple fix would be to teleport the player back into the game after the data has failed to load.

If err then game:getservice(“TeleportService”):Teleport(plr) end I don’t remember the exact syntax for teleport service and I’m on phone but I think u get the idea.

And then if the player’s data fails to save. You can implement a retry system. Note datastores have limits on how frequently you can call getasync/setasync. U can check the docs but I think the limit is (60 x numofplayers in server) + 10? So yeah make sure there is a cooldown in that retry system.

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