DataStore that saves some other values other than leaderstats

So i want to make a datastore system that saves how much money you have in leaderstats and some other values of other parts but it only saves the leaderstats and not the values of the other parts.
It is a bool value and i have tried changing the script in many ways but it doesn’t work

	local DSS = game:GetService("DataStoreService")
	 
	local datastore = DSS:GetDataStore("GeneralSaveData", "Players")
	 
	function generateDataKey(player)
	    local ret = "uid_" .. player.userId
	    return ret
	end
	 
	function generateDataTable(player)
	    local dataTable = {
	        Coins = player.leaderstats.Coins.Value,
            game.ServerStorage.ValuesToSave.Barrier1.Value = data.Barrier1
            game.ServerStorage.ValuesToSave.Barrier2.Value = data.Barrier2	    
            game.ServerStorage.ValuesToSave.Barrier3.Value = data.Barrier3	
            }
	    return dataTable
	end
	 
	function saveDataForPlayer(player)
	    local key = generateDataKey(player)
	    local data = generateDataTable(player)
	    datastore:SetAsync(key, data)
	end
	 
	function inputDataToPlayer(player, data)
	    player.leaderstats.Coins.Value = data.Coins
        game.ServerStorage.ValuesToSave.Barrier1.Value = data.Barrier1
        game.ServerStorage.ValuesToSave.Barrier2.Value = data.Barrier2	    
        game.ServerStorage.ValuesToSave.Barrier3.Value = data.Barrier3	
	end
	 
	function loadDataForPlayer(player)
	    local key = generateDataKey(player)
	    local data = datastore:GetAsync(key)
	    inputDataToPlayer(player, data)
	end
	 
	game.Players.PlayerAdded:Connect(function(player)
	    loadDataForPlayer(player)
	    player.leaderstats.Coins.Value.Changed:Connect(function()
		        saveDataForPlayer(player)	  
    end)
end)
	 
	game.Players.PlayerRemoving:Connect(saveDataForPlayer)

I cant find any errors in the script, if someone can help me fix it i would appreciate it really really much.
Thanks.

I accidentally posted that my cat walked on my keyboard sorry

Its the same, it still saves just the money of the leaderstats and doesn’t save the other values too

It seems like you may be assigning the value of an object in the serverstorage to the variable, instead of assigning the variable to the value of the object upon saving. In your generateDataTable function, try this:

	function generateDataTable(player)
	    local dataTable = {
	        Coins = player.leaderstats.Coins.Value,
            Barrier1 = game.ServerStorage.ValuesToSave.Barrier1.Value,
            Barrier2 = game.ServerStorage.ValuesToSave.Barrier2.Value,
            Barrier3 = game.ServerStorage.ValuesToSave.Barrier3.Value,
            }
	    return dataTable
	end

Please let me know if it helps!

2 Likes

It sounds like you want to create a data storage system that saves the player’s money and other values in their leaderstats, but also saves other values from other parts of the game.

One way to do this would be to create a custom data store that can save multiple values and associate them with a player. You can use the leaderstats to store the player’s money, and then use the custom data store to store the other values. When the player logs in or re-joins the game, you can retrieve their values from the custom data store and set the appropriate values in the leaderstats and other parts of the game.

Here’s an example of how you can create a custom data store that can save multiple values:

-- Create a table to store player data
local PlayerData = {}

-- Function to save player data
function SavePlayerData(player, money, otherValue1, otherValue2)
    -- Create a table to store the player's data
    local playerData = {}
    playerData.Money = money
    playerData.OtherValue1 = otherValue1
    playerData.OtherValue2 = otherValue2
    -- Associate the player's data with the player's ID
    PlayerData[player.UserId] = playerData
end

-- Function to load player data
function LoadPlayerData(player)
    -- Get the player's data by their ID
    local playerData = PlayerData[player.UserId]
    -- Set the player's money in the leaderstats
    player.leaderstats.Money.Value = playerData.Money
    -- Set the other values in the appropriate places
    -- ...
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.