Need help with datastore

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    im trying to save the players’ “Clicks” Value, the leaderstats works but the datastore doesn’t.

  2. What is the issue? Include screenshots / videos if possible!
    doesn’t save.
    it also doesn’t print anything and there is no error in the output.

. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
read posts on the devforum.

my leaderstats script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	
	local clicks = Instance.new("IntValue",leaderstats)
clicks.Name = "Clicks"
end)

my data store script:

--//Variables\\--

local DataStoreService = game:GetService("DataStoreService")
local SavedClicks = DataStoreService:GetDataStore("SavedClicks")



local function SaveData(player)
	local DataToSave = {
		player.leaderstats.Clicks.Value -- this is the data that we want to save.
	}
	
	
	local success, err = pcall(function()
		SavedClicks:SetAsync(player.UserId,DataToSave) -- save the data
		
	end)
	if success then
		print("Data Has Been Saved!")
		
	else -- if the script doesn't error...
		print("Data Has not been saved!")
		warn(err)
	end
end

game.Players.PlayerRemoving:Connect(function(player)--if a player leaves the game...
	local success, err = pcall(function()
		SaveData(player)--save the data!
	end)
	
	if success then
		print("Data Successfully Saved") --tell us whether th data was saved or not
	else
		print("data did not save :(")
	end
	
end)

If you are testing it in studio, this is likely to happen because of the way studio works. Maybe add a BindToClose or test it on a real server.

1 Like

You need to Get the async on the first pcall function (which is inside the playeradded function), and SetAsync at the second pcall function (inside of the playerremoving function)

if it doesn’t work replace the GetAsync in the first pcall function line with

SavedClicks:GetAsync(player.UserId.."-Clicks)

And the SetAsync in the second pcall function line with

SavedClicks:SetAsync(player.UserId.."-Clicks",player.leaderstats.Clicks.Value)

Hope it helps.

2 Likes

You can use this because studio or single Roblox servers may close after the player leaves, that will force the server to wait 4 seconds to close and will complete the SetAsync request.

game:BindToClose(function()
     wait(4) -- this will force the "SetAsync" to complete
end)
1 Like

That isn’t neccisary, that just changes the format of the key.

Also make sure you make a GetAsync request in a PlayerAdded event.

Kimathi4theWin already did that, I only showed him what you might want to change inside of the playeradded, and yes I agree it’s not so necessary, but this is usually the way I do it.

Please try this, I hope it helps for what you need. The prior repliers mentioned some of what is added here, they just didn’t tell you how to format it or where to place it for it to work. You may or may not already be knowledgeable enough to know but in case you don’t here you go. I am very new so seeing where it is written into the existing script is super helpful for me.

local DataStoreService = game:GetService("DataStoreService")
local SavedClicks = DataStoreService:GetDataStore("SavedClicks") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data

	local DataToSave = {
		player.leaderstats.Clicks.Value -- First value from the table
		
	}

	local success, err = pcall(function()
		SavedClicks:SetAsync(player.UserId, DataToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end


game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves the game
	saveData(player) -- Save the data
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do
		saveData(player) -- Save the data
	end


end)


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

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

	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = leaderstats

	

	local data
	local success, err = pcall(function()

		data = SavedClicks:GetAsync(player.UserId)

	end)

	if success then

		Clicks.Value = data[1]
	

	else
		print("The player has no data!") -- The default will be set to 0	
	end

end)
local DataStoreService = game:GetService("DataStoreService")
local SavedClicks = DataStoreService:GetDataStore("SavedClicks")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
	
	local clicks = Instance.new("IntValue")
    clicks.Name = "Clicks"
    clicks.Parent = leaderstats

    local success, result = pcall(function()
        return SavedClicks:GetAsync(player.UserId)
    end)

    if success then
        clicks.Value = result[1]
    else
        warn(result)
    end
end)

game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
   xpcall(function()
      SavedClicks:UpdateAsync(
         player.UserId,
         function(old)
            return {player.leaderstats.Clicks.Value}
         end)
      end, warn)

      if #game.Players:GetPlayers() > 1 then
         wait(11)
      end
   end
end)