Attempt to index nil (DATA STORAGE)

No clue what’s going on, there’s constantly the error of:
ServerScriptService.SaveService:39: attempt to index nil with 'statsOne'

Anyone got any solution to this? Whole code is here:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	local statsOne = Instance.new("IntValue")
	local statsTwo = Instance.new("IntValue")
	local Coins = Instance.new("NumberValue", player)

	local swordbought = Instance.new("Folder", player)
	swordbought.Name = "SwordsBought"

	Coins.Name = "Coins"

	local statsFour = Instance.new("NumberValue")
	statsFour.Name = "statsFour"
	statsFour.Parent = leaderstats

	local Died = Instance.new("BoolValue", player)
	Died.Value = false
	Died.Name = "Died"

	statsOne.Name = "statsOne"
	statsTwo.Name = "statsTwo"
	leaderstats.Name = "leaderstats"
	statsOne.Parent = leaderstats
	statsTwo.Parent = leaderstats


	local playerUserId = "Player_"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)

	if success then
		statsOne.Value = data.statsOne
		statsTwo.Value = data.statsTwo
		Coins.Value = data.Coins
		statsFour.Value = data.statsFour
		--EXP_F.Value = data.EXP_F
	end


end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		statsOne = player.leaderstats.statsOne.Value;
		statsTwo = player.leaderstats.statsTwo.Value;
		Coins = player.Coins.Value;
		statsFour = player.leaderstats.statsFour.Value;
		--EXP_F = player.leaderstats.EXP_F.Value
	}

	local success,errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving!")
		warn(errormessage)
	end

end)
1 Like

If the player has never had data saved for them before, then the initial value in their data store is nil. When you are loading for a new player, the value in their Datastore will always be nil, and there will be no statsOne, unless you have saved for that player already before loading. This needs to be accounted for.

Your PlayerAdded function will attempt to load a statsOne key from the datastore when that entry is nil.

1 Like

Ahh I see, how would I fix this issue at all?

It depends what your values are. I’m going to assume that your statsOne and statsTwo for a fresh player should be 0. Then in your load function you can do:

if success then
	if data ~= nil then
		statsOne.Value = data.statsOne
		statsTwo.Value = data.statsTwo
	else
		statsOne.Value = 0
		statsTwo.Value = 0
	end

	Coins.Value = data.Coins
	statsFour.Value = data.statsFour
	--EXP_F.Value = data.EXP_F
end
7 Likes

I added that ty, however whenever I leave my prints (Data successfully saved) / (There was an error while saving) didn’t print, which leads me to believe there’s also an issue with the PlayerRemoving half, however there’s no errors this time. Anything ya might think it could be?

From a first look it seems the dictionary format you’re saving with is off. You’re supposed to use brackets as well for the keys.

local dict = {
	["A"] = 3,
	["B"] = 5,
	["C"] = 8
}

As for the semicolons, I haven’t seen that done before, but it seems to work either way. And if you want variable names, such as statsOne.Name then the following works with variable names:

local a = "A"
local dict = {
	[a] = 3,
	["B"] = 5,
	["C"] = 8
}

Alright gotcha, thank you very much for the help! Marked as Solution.

1 Like