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

How to automatically save all values in a folder? (IntValue, BoolValue and StringValues)

Asked by 5 years ago
Edited 5 years ago

I would like to save all the values in a folder without them directly in the script. So if a new value is added during the game this should also be saved and loaded

my 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 = {

        TestValue = game.Workspace.TestFolder.TestValue.Value
        --I thought about doing something like Data = game.Workspace.TestFolder:GetChildren()

    }
    return dataTable
end

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

function inputDataToPlayer(player, data)
    if data ~= nil then


    game.Workspace.TestFolder.TestValue.Value = data.TestValue

end


end
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) 
    wait ()
    while true do
        wait (60)
        saveDataForPlayer(player) 
    end
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer) 


Is there a way to do this?

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
5 years ago
Edited 4 years ago

Hello, MageMasterHD!

You shuld try this:

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 = {} -- Create a empty table, to use table.Insert
    for i,v in pairs(game.Workspace.TestFolder:GetChildren()) do --Gets all the childrens of the folder
        table.Insert(dataTable, v.Value) -- Adds the value on the table
    end
    return dataTable
end

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

function inputDataToPlayer(player, data)
    if data ~= nil then


    game.Workspace.TestFolder.TestValue.Value = data.TestValue

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) 
    wait ()
    while true do
        wait (60)
        saveDataForPlayer(player) 
    end
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer) 


Useful links:

https://developer.roblox.com/articles/Lua-Libraries/table#table.insert

Good Luck with your games

0
@Leamir I believe one of the ends (Line 33 or 34) was unnecessary; It gave me a script error when I tried to use this script. Thanks, anyway! BetaTrivus -2 — 5y
Ad

Answer this question