Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I create a Data Store that uses :GetChildren() ?

The generateDataTable function correctly puts all of the items in the dataTable with the :GetChildren() (that I know of)

But I was wondering how am I able to load the values from the dataTable under the loadDataForPlayer function

Thank you in advance if anyone can help

The script V

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)
	print("Generating data table")
	local playerName = player.Name
	local Stats = game.ReplicatedStorage.Stats:WaitForChild(playerName)
    local dataTable = {
        Coins = Stats.coins.Value,
		
    }
	local chars = Stats.items:GetChildren()
	for i,v in pairs(chars) do
		local char=chars[i]
		dataTable[char.Name] = char.Name.Value
	end
    return dataTable
end

function saveDataForPlayer(player)
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    datastore:SetAsync(key, data)
end

function inputDataToPlayer(player, data)
	local playerName = player.Name
	local Stats = game.ReplicatedStorage.Stats:WaitForChild(playerName)
    Stats.coins.Value = data.Coins
end

function loadDataForPlayer(player)
    local key = generateDataKey(player)
    local data = datastore:GetAsync(key)
    inputDataToPlayer(player, data)
end

game.Players.PlayerAdded:connect(function(player)
	local playerName = player.Name
    loadDataForPlayer(player)
    game.ReplicatedStorage.Stats:WaitForChild(playerName).coins.Changed:connect(function()
        saveDataForPlayer(player) 
    end)
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer)

1 answer

This discussion is preserved as it was. New answers, comments, and votes are closed.