SetAsync keeps failing

I’m trying to make a script that saves the players data. The script works by accessing the IntValues in the player’s leaderstats folder. Then takes the values stored in the IntValues and stores it inside a table named generalData. The table is then saved using SetAsync.

For some reason setAsync keeps failing to save. It keeps giving the error message.

Players.PlayerRemoving:Connect(function(player)
	print("player left")
	local ID = "GeneralSave:"..player.UserId
	local generalData = {}
	for i, stat in ipairs(player.leaderstats:GetChildren())do
		table.insert(generalData, stat.Value)
	end
	
	local success, response = pcall(function()
		generalDataStore:SetAsync(ID, generalData)
	end)
	if not success then
		warn(response)
	end
end)

The output keeps resulting in:
image

Update:
I changed the output of the error message I have displayed the new output.

Update 2:

OMG I JUST FREAKIN REALIZED THAT I MADE ROOKIE MISTAKE OF

local generalDataStore = DataStoreService:GetDecendants("GeneralDataStore")

INSTEAD OF

local generalDataStore = DataStoreService:GetDataStore("GeneralDataStore")

I would like to thank Forummer for bringing that part of the script to my attention or else I probably would not have figured this out until 10 years later or something.

I would also like to thank those who replied because yall provided valuable information to make this script more efficient.

1 Like

Do you’ve API services enabled?

Edit ^

Plus you haven’t added the player inside the playerremoving parameters, that will grab what player is being removed and then will be able to save data by that

1 Like

try this

Players.PlayerRemoving:Connect(function(p)
	print("player left")
	local ID = "GeneralSave:"..p.UserId
	local generalData = {}
	for i, stat in ipairs(p.leaderstats:GetChildren())do
		table.insert(generalData, stat.Value)
	end
	
	local success, error = pcall(function()
		generalDataStore:SetAsync(ID, generalData)
	end)
	if not success then
		warn("Error")
	end
end)

Basically what I stated ^ Putting the player inside the parameters

I did and it still doesn’t work

Did you change it so that it is grabbing the player parameter?

Im not sure what you mean by that

Hello, please print the error.

You’re actually doing alot wrong, let me explain

Instead of having line 13 set as warn("Error"), you could replace it with warn(tostring(error). Then screenshot what the error outputted.

I’m not sure why you arent using the set function of a pcall function, when printing your error do.

Players.PlayerRemoving:Connect(function(p)
	print("player left")
	local ID = "GeneralSave:"..p.UserId
	local generalData = {}
	for i, stat in ipairs(p.leaderstats:GetChildren())do
		table.insert(generalData, stat.Value)
	end
	
	local success, response = pcall(function()
		generalDataStore:SetAsync(ID, generalData)
	end)
	if not success then
		warn("Error, response: " .. response)
	end
end)

The 2nd param of success, response/error, gives you the error of what you are facing.

2 Likes

So firstly, when each player is being removed it’ll override the previous table, therefore you’ll need to keep adding new arrays for each player which is stupid, I suggest calling a local function so that it’ll save each players data without overriding the other.

I suggest either have it so that it adds new arrays or just simply make a local function so it doesn’t override the table you’re trying to save.

You don’t need to do tostring, just do warn(response) it’ll print the error and what line it’s erroring at which will be clickable and faster to see where it is.

After the player leaves, all of the instances including leaderstats are removed. Have you tried saving the leaderstats by referring to an array just in case the player leaves? If you saved the leaderstats in an array before the player leaves, you could save the data with the array.

Here’s an example:

local savedLeaderstats = {}

Players.PlayerAddedConnect(function(player)
	print("player joined")
    local leaderStatData = {}
    savedLeaderstats[player.UserId] = leaderStatData

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

   local playerStats = {"Stage"} -- You could change the table to something you like. It may not be "Stage" you want to add.

    for i, stat in pairs(playerStats) do
       local statInst = Instance.new("IntValue", leaderstats)
       statInst.Name = stat
       statIns.Changed:Connect(function()
           leaderStatData[stat] = statIns.Value
       end)

       leaderStatData[stat] = statIns.Value
    end
end)

Players.PlayerRemoving:Connect(function(player)
	print("player left")
	local ID = "GeneralSave:"..player.UserId
	local generalData = {}
	for stat, value in pairs(savedLeaderstats[player.UserId])do
		generalData[stat] = value
	end

    savedLeaderstats[player.UserId] = nil
	
	local success, error = pcall(function()
		generalDataStore:SetAsync(ID, generalData)
	end)
	if not success then
		warn("Error")
	end
end)

maybe you didnt turn player.UserId into a string?
local ID = "GeneralSave:"..tostring(player.UserId)

You don’t need to make it a string.

Non-string literal concatenations are automatically converted into strings.

1 Like

Your warning indicates that generalDataStore is a nil value, in other words it hasn’t been initialized.