Hey guys, im having an issue with my datastore. So i plan on saving alot of data, and i want to do it on one key, the player UserId. To do this, i make a table and use Http Service to encode and decode it (which works, i tested it), but it appears it does not save correctly. Here is the code:
-- [ SERVICES ] -- local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local Http = game:GetService("HttpService") -- [ DATASTORES ] -- local Data = DataStoreService:GetDataStore("Data") Players.PlayerAdded:Connect(function(Player) -- Leaderboard local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = Player -- Coins local Coins = Instance.new("IntValue") Coins.Name = "Coins" Coins.Parent = leaderstats -- House local House = Instance.new("IntValue") House.Name = "House" House.Value = 1 House.Parent = Player local PlotType = Instance.new("StringValue") PlotType.Name = "PlotType" PlotType.Parent = Player local HouseModel = Instance.new("StringValue") HouseModel.Name = "HouseType" HouseModel.Parent = Player -- Player Data local PlayerData = { Coins = Coins.Value, PlotType = PlotType.Value } local Encode local Success = false local UserId = Player.UserId while Success == false do local suc,err = pcall(function() Encode = Data:GetAsync(UserId) end) if suc then Success = true else print("Error loading player data") warn(err) end end local Decode local _Success = false while _Success == false do local suc,err = pcall(function() Decode = Http:JSONDecode(Encode) end) if suc then _Success = true end end end) Players.PlayerRemoving:Connect(function(Player) local leaderstats = Player.leaderstats local UserId = Player.UserId local SavedData = { Coins = leaderstats.Coins.Value, PlotType = Player.PlotType.Value } local Encode local _Suc = false while _Suc == false do local Success,Error = pcall(function() Encode = Http:JSONEncode(SavedData) end) if Success then _Suc = true end end local Success = false while Success == false do local suc,err = pcall(function() -- It seems to be here Data:SetAsync(UserId,Encode) -- This doesn't seem to run, and the code seems to stop here for some reason end) if suc then print("Success!") Success = true else print("There was an error saving data") warn(err) end end end)
You don't need HttpService to save Data using Leaderstats, if I were you watch this video; https://www.youtube.com/watch?v=DkYupSBUpes
I would give you the code but apparently spoonfeeding isn't allowed on Scripting Helpers!
Good Luck, have a good day! (also mark this as solved if it worked)