Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I use get a datastore to save multiple children? [closed]

Asked by
Viking359 161
6 years ago

This question already has an answer here:

Data Stores(Saving Tools) With Loops?

I'm new to scripting with datastores, so this is probably a simple question, but how do I get my datastore to save all of the children of a certain data model?

--the datastore script
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 = {
        Gold = player.leaderstats.Gold.Value,
        Weapons = player.weapons:GetChildren()--how would I save all of the children of weapons?
    }
    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.Gold.Value = data.Gold
local weps = player.weapons
--maybe a for loop here?
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)
for index, tool in pairs(player.weapons:GetChildren()) do
    local a = game.ServerStorage.Weapons:FindFirstChild(tool.Name)
    a:Clone()
   a.Parent = player.Backpack, player.StarterGear
end
    end)

game.Players.PlayerRemoving:Connect(saveDataForPlayer) 
0
you can't save instances in datastore creeperhunter76 554 — 6y

Marked as Duplicate by Goulstem

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?