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?
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