Hi guys! In the player, I create a model with different values in them. Instead of individually saving each value, I am trying to put all the values into a table, save that and then load that back into the player when they join the game. I am not 100% about this, and it is not working :/ Any ideas? Thank you very much :) PS: I want to save it as the values change, because if you do it on "PlayerRemoving", sometimes the player can crash and it wont save!
game.Players.PlayerAdded:connect(function(Player) model = Instance.new("Model") model.Name = "PlayerModel" model.Parent = Player local value = Instance.new("BoolValue") value.Name = "ValueOne" value.Parent = model local lol = Instance.new("IntValue") lol.Name = "ValueTwo" value.Parent = model local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerModel") local tab = dataStore:GetAsync("PlayerModel") for _, child in pairs(model:GetChildren()) do for _, saved in pairs(tab) do if rawequal(saved.Name, child.Name) then child.Value = saved.Value end end end end) local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerModel") local tab = {} for _, v in pairs(model:GetChildren()) do tab[#tab + 1] = {Name = v.Name, Value = v.Value} end dataStore:SetAsync("PlayerModel", tab)
local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerModel") local utility = game:GetService("HttpService") game.Players.PlayerAdded:connect(function(player) local model = Instance.new("Model") model.Name = "PlayerModel" model.Parent = player local value = Instance.new("BoolValue") value.Name = "ValueOne" value.Parent = model local lol = Instance.new("IntValue") lol.Name = "ValueTwo" lol.Parent = model local tab = dataStore:GetAsync(player.userId) or {} for _, child in pairs(model:GetChildren()) do for _, saved in pairs(utility:JSONDecode(tab)) do if rawequal(saved.Name, child.Name) then child.Value = saved.Value end end child.Changed:connect(function(property) local tab = {} for _, v in pairs(child.Parent:GetChildren()) do tab[#tab + 1] = {Name = v.Name, Value = v.Value} end local encoded = utility:JSONEncode(tab) dataStore:SetAsync(player.userId, encoded) end) end end) game.OnClose = function() wait(2) end