I have a folder in my game, that contains string values for the player. How would i save these using a datastore? I’ve tried looking everywhere for an answer, but I can’t find any. Thanks!
this isnt really efficient but you could:
- create a table.
- for each StringValue, assign an index in the table to the value of the StringValue.
- use HttpService to JSONEncode the table that you have now.
- store that in datastore.
to get the data back, just JSONDecode it & it will spit out the table that you created & encoded.
Loop through all the StringValues and combine them into a dictionary:
local Folder = ...;
local SaveData = {};
for _,StringValue in pairs(Folder:GetChildren()) do
if (StringValue:IsA('StringValue')) then
SaveData[StringValue.Name] = StringValue.Value;
end
end
Save using SetAsync
/UpdateAsync
and load using GetAsync
:
To save a folder with string values for a player in a Roblox DataStore, you just need to go through the folder’s contents, grab the string values, and save them to the DataStore. Then, when you’re loading the data, you can recreate those string values in the folder. Here’s how to do it:
Saving the folders string values:
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerDataStore")
game.Players.PlayerRemoving:Connect(function(player)
local folder = player:FindFirstChild("YourFolderName") -- Replace with your folder's name
if folder then
local dataToSave = {}
for _, stringValue in pairs(folder:GetChildren()) do
if stringValue:IsA("StringValue") then
dataToSave[stringValue.Name] = stringValue.Value
end
end
-- Save the data to the DataStore
local success, err = pcall(function()
playerDataStore:SetAsync(player.UserId, dataToSave)
end)
if not success then
warn("Failed to save data for player " .. player.Name .. ": " .. err)
end
end
end)
Loading the folders string values:
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "YourFolderName" -- Replace with your folder's name
folder.Parent = player
local success, data = pcall(function()
return playerDataStore:GetAsync(player.UserId)
end)
if success and data then
for name, value in pairs(data) do
local stringValue = Instance.new("StringValue")
stringValue.Name = name
stringValue.Value = value
stringValue.Parent = folder
end
elseif not success then
warn("Failed to load data for player " .. player.Name .. ": " .. data)
end
end)
Hope this helps.
i love how the 3 replies above this are just:
- the idea
- the implementation
- the full implementation
Thanks for the help! This works perfectly
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.