Im trying to make my datastore save each folder’s content. It works fine when handling one folder, but all my attempts to make it save each string to different folders, it doesnt. Tried a bunch of different things, but now im out of time, so i decided to make this post, see how i can improve rather than making a bunch of different datastores.
local datastore = game:GetService("DataStoreService"):GetDataStore("test")
function save(player)
local datatosave = {}
for i, v in pairs(player:FindFirstChild("Data"):GetChildren()) do
if v:IsA("Folder") then
for i2, v2 in pairs(v:GetChildren()) do
datatosave[v2.Name] = v2.Value
end
end
end
datastore:SetAsync(player.UserId, datatosave)
end
game.Players.PlayerAdded:Connect(function(player)
local datafolder = Instance.new("Folder", player)
datafolder.Name = "Data"
local keybinds = Instance.new("Folder", datafolder)
keybinds.Name = "Keybinds"
local rollkey = Instance.new("StringValue", keybinds)
rollkey.Name = "rollkey"
rollkey.Value = "Enum.KeyCode.Space"
local runkey = Instance.new("StringValue", keybinds)
runkey.Name = "runkey"
runkey.Value = "Enum.KeyCode.Space"
local jumpkey = Instance.new("StringValue", keybinds)
jumpkey.Name = "jumpkey"
jumpkey.Value = "Enum.KeyCode.F"
local inventory = Instance.new("Folder", datafolder)
inventory.Name = "Inventory"
local loadedData = datastore:GetAsync(player.UserId)
if loadedData then
print(loadedData)
for i,v in pairs(datafolder:GetChildren()) do
if v:IsA("Folder") then
for i2,v2 in pairs(v:GetChildren()) do
for i3, v3 in pairs(loadedData) do
if v[i3] then
v[i3].Value = v3
end
end
end
end
end
end
end)
game:BindToClose(function() --NORMAL SAVE GAME CLOSE
for i, v in pairs(game.Players:GetChildren())do
save(v)
end
end)
game.Players.PlayerRemoving:Connect(function(player) --NORMAL SAVE PLAYER QUIT
save(player)
end)
Maybe if i could get the string’s parent on top? my attempts didnt work.
(I dont even know if the keybinds work, but im first trying to figure out datastores)