DataStore is not saving

Hello there! I am trying to make a DataStore Script but it won’t save. At first, I thought the problem might be with GetAsync() but I’ve printed it and it is nil. So the problem is with :SetAsync().

Script:


---------------------------------------------- DATASTORE ----------------------------------------------
local Settings = require(script.Settings)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore(Settings["DataStoreName"])

game.Players.PlayerAdded:Connect(function(player)
	local ReceivedData
	local Folder = Instance.new("Folder",player)
	Folder.Name = "leaderstats"
	local DataValue = Instance.new("IntValue",Folder)
	DataValue.Name = Settings["DataStoreCurrency"]
	DataValue.Value = Settings["DefaultMoney"]
	
	local Success, Error = pcall(function()
		local ReceivedData = DataStore:GetAsync(player.UserId)
	end)
	
	if Success then
		if ReceivedData ~= nil then
			DataValue.Value = ReceivedData
		end
	else
		if Settings["KickPlayerIfDataStoreFailedToLoad"] then
			player:Kick(Settings["DataStoreFailedToLoadMessage"])
		else
			warn("DataStore failed to load for "..player.Name..". Error Message: "..Error)
		end
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local ValueToSave = player.leaderstats[Settings["DataStoreCurrency"]].Value
	
	local Success, Error = pcall(function()
		DataStore:SetAsync(player.UserId,ValueToSave)
	end)
		
	if Success ~= true then
		warn("DataStore failed to save for "..player.Name..". | DataStore: "..Settings["DataStoreName"].." | Currency: "..Settings["DataStoreCurrency"].." | Value to Save: "..ValueToSave)
	end
end)

ModuleScript:

local Settings = {

["DataStoreName"] = "ChangeThis",

["DataStoreCurrency"] = "Cash",

["DefaultMoney"] = 50,

["DataStoreFailedToLoadMessage"] = "DataStore failed to load!",

["KickPlayerIfDataStoreFailedToLoad"] = false

}

return Settings

9th line of the event, game.Players.PlayerAdded:

    local Success, Error = pcall(function() 
        local ReceivedData = DataStore:GetAsync(player.UserId)
    end)

You are defining ReceivedData as a local variable here, which is causing the data to be destroyed the minute you exit the pcall. Just remove the local keyword, and :GetAsync should be printing something, unless there is another logical error I have not caught.

2 Likes

The datastore key needs to be a string. This is the GetAsync and SetAsync fixed:

local ReceivedData = DataStore:GetAsync(tostring(player.UserId))
DataStore:SetAsync(tostring(player.UserId),ValueToSave)
4 Likes

This can actually be simplified down to this code:

local Success, Data = pcall(DataStore.GetAsync, DataStore, player.UserId)

if Success was true then Data would be the data
otherwise it’s the error message

4 Likes

Yes. The fact that pcall is a callback.

-- Anonymous function method
local success, result = pcall(function ()
    return DataStore:GetAsync(player.UserId)
end)

-- Direct wrap and call method
local success, result = pcall(DataStore.GetAsync, DataStore, player.UserId)
1 Like