ServerScriptService.leaderstatsSave:13: attempt to index nil with number

I’m making a datastore script, but it didn’t work. And I couldn’t understand what the problem is.
Script:

local dataStoreService = game:GetService("DataStoreService")

local statsStore = dataStoreService:GetDataStore(“StatStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = game.ServerScriptService.leaderstats
local wallet = leaderstats:FindFirstChild(“Wallet”)

local recieved, notRecieved = pcall(function()
	stat = statsStore:GetAsync("UserId-"..player.UserId)
end)

if recieved then
	wallet.Value = stat[1]
	print("Stat was recieved")
else
	print("Stat was not recieved")
end

while true do wait(50)
	statsStore:SetAsync("UserId-"..player.UserId{wallet.Value})
	print("Auto saved.")
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player.leaderstats
local wallet = leaderstats.Wallet

local saved, notSaved = pcall(function()
	statsStore:SetAsync("UserId-"..player.UserId{wallet.Value})
end)

if saved then
	print("Stats was saved")
else
	print("Stats failed to save")
end

end)

The number 13 is at “wallet.Value = stat[1]”

Your recieved variable contains a true/false value telling you whether your pcall ran with or without error; there’s no guarantee the player has existing data. To fix this, first check to see if stat exists:

if stat then
wallet.Value = stat[1]
print("Stat was received")
end

Another thing I noticed is it appears you have a syntactic error when you use the :SetAsync method, you’re missing a , between UserId and your table.

Hope this helps!

this should work

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local dataStoreService = game:GetService("DataStoreService")

local statsStore = dataStoreService:GetDataStore("StatStore")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = ServerScriptService.leaderstats:Clone()
	leaderstats.Parent = player
	local wallet = leaderstats.Wallet
	
	local stat
	local recieved = pcall(function()
		stat = statsStore:GetAsync("UserId-" .. player.UserId)
	end)
	
	if recieved then
		if stat then
			wallet.Value = stat[1]
			print("Stat was recieved")
		else
			wallet.Value = 0  -- default for new players
			print("No Stat. New player")
		end
	else
		print("Stat was not recieved")
	end
	
	spawn(function ()
		while true do 
			wait(50)
			statsStore:SetAsync("UserId-" .. player.UserId, {wallet.Value})
			print("Auto saved.")
		end
	end)

end)

Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player.leaderstats
	local wallet = leaderstats.Wallet
	
	local saved = pcall(function()
		statsStore:SetAsync("UserId-" .. player.UserId, {wallet.Value})
	end)
	
	if saved then
		print("Stats was saved")
	else
		print("Stats failed to save")
	end

end)

I’m assuming your leaderstat is on ServerScriptService like this
image

1 Like

Thank you, it works. (found another solution too)

Thank you but it did not work, sorry if I wasted your time-