I am creating a new game and I have made a Datastore to save various value relating to character customization. I have a folder in serverstorage which is cloned onto the player when they join the game. The script gives no errors and prints everything that it should. However, when rejoining the game the value is nil. Any help would be appreciated.
local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("CharacterCustomization1")
local loaded = {}
game.Players.PlayerAdded:Connect(function(player)
local success, value = pcall(DataStore.GetAsync, DataStore, player.UserId)
if success == false then player:Kick("DataStore failed to load") return end
if value == nil then print("No Data") end
local data = value or {}
print(player.UserId,"Loaded:", data)
for i, folder in game.ServerStorage.PlayerData:GetChildren() do
local subData = data[folder.Name] or {}
local clone = folder:Clone()
for i, child in clone:GetChildren() do
child.Value = subData[child.Name] or child.Value
end
clone.Parent = player
end
loaded[player] = true
end)
game.Players.PlayerRemoving:Connect(function(player)
if loaded[player] == nil then return end
local data = {}
for i, folder in game.ServerStorage.PlayerData:GetChildren() do
local subData = {}
for i, child in player[folder.Name]:GetChildren() do
subData[child.Name] = child.Value
end
data[folder.Name] = subData
folder:Destroy()
end
local success, value = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
print(player.UserId,"Saved:", data)
loaded[player] = nil
end)
game:BindToClose(function()
while next(loaded) ~= nil do
task.wait()
end
end)