so the script i made is supposed to save a table with items names, and when player joins it should find items from the table in purchasables model (which is a model that has all purchasable items), and put them into purchased model (where bought items are stored) but it surprisingly doesn't work. however im not getting any errors. Heres the script:
local datastore = game:GetService("DataStoreService") local ds2 = datastore:GetDataStore("DS2") local purchasables = game.ServerStorage.PURCHASABLES:GetChildren() game.Players.PlayerAdded:Connect(function(plr) local success,err = pcall(function() for i,v in pairs(purchasables) do if v.Name == val then v.Parent = game.Workspace.PURCHASED end end end) if success then print("Your Items Have Been Loaded.") else print("Your Items Loading Has Failed.") warn(err) end end) game.Players.PlayerRemoving:Connect(function(plr) for i,v in pairs(game.Workspace.PURCHASED:GetChildren()) do tab,val = {},v.Name table.insert(tab,val) end local success,err = pcall(function() for i,v in pairs(purchasables) do ds2:SetAsync(plr.UserId,val) end end) if success then print("Your Items Have Been Saved.") else print("We Got An Error While Saving Your Items.") warn(err) end end)
well the best thing to do is to save the table as JSON, you can make your own JSON parser, or you can use HTTP service function parsers
for instance:
local http= game:GetService("HTTPService") local data = {name = "badcc", vehicles = {"lambo", "benz", "tesla"} }; local json = http:JSONEncode(data) --turns table to a json object local data2 = http:JSONDecode(json) -- turns the json object into a lua object, like "data"
remember, the JSON returned from http:JSONEncode
is just a lua string, which means you can save it to datastore
when you call http:JSONEncode(data)
it will retun something like this
"{name = "badcc", vehicles=["lambo", "benz", "tesla"]}"
you can find out more about the HTTP service
here
this is what i have done so far with the json
local datastore = game:GetService("DataStoreService") local http = game:GetService("HttpService") local ds2 = datastore:GetDataStore("DS2") local purchasables = game.ServerStorage.PURCHASABLES:GetChildren() game.Players.PlayerAdded:Connect(function(plr) local success,err = pcall(function() for i,v in pairs(purchasables) do data2 = http:JSONDecode(json) if v.Name == data2 then v.Parent = game.Workspace.PURCHASED end end end) if success then print("Your Items Have Been Loaded.") else print("Your Items Loading Has Failed.") warn(err) end end) game.Players.PlayerRemoving:Connect(function(plr) for i,v in pairs(game.Workspace.PURCHASED:GetChildren()) do data = {v.Name} json = http:JSONEncode(data) end local success,err = pcall(function() for i,v in pairs(purchasables) do ds2:SetAsync(plr.UserId,json) end end) if success then print("Your Items Have Been Saved.") else print("We Got An Error While Saving Your Items.") warn(err) end end)
rip
--Try this local dataStore = game:GetService("DataStoreService") local DS = dataStore:GetDataStore("DS2") local purchasables = game.ServerStorage:WaitForChild("PURCHASABLES"):GetChildren() local purchased = workspace:WaitForChild("PURCHASED") local purchasedTBL = purchased:GetChildren() game.Players.PlayerAdded:Connect(function(plr) local gameData = DS:GetAsync(plr.UserId) for _,v in pairs(purchasables) do if gameData == nil then return end --New Player gameData[v].Parent = purchased end end) game.Players.PlayerRemoving:Connect(function(plr) local saveData = {} for _,v in pairs(purchased) do table.insert(saveData,v) end DS:SetAsync(plr.UserId, saveData) end) game:BindToClose(function() --Prevent Data Loss, if unexpected shutdown for _,v in pairs(game.Players:GetPlayers()) do v:Kick("Closing") end game:GetService("Players").PlayerAdded:Connect(function(plr) plr:Kick("Closing") end) end)