Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local datastore = game:GetService("DataStoreService"):GetDataStore("p0vdStore")
- --this is just for testing
- function printTable(myTable)
- for i, v in pairs(myTable)do
- print(i,v)
- end
- end
- game.Players.PlayerAdded:Connect(function(player)
- -- datastore:RemoveAsync("user-"..player.userId)
- --create a new folder that stores the players data
- --and put that folder into the playerInformation folder
- local dataFolder = game.ReplicatedStorage.Data:Clone()
- dataFolder.Name = player.Name
- dataFolder.Parent = game.ReplicatedStorage.PlayerInformation
- --try to get data from datastore
- local success, data = pcall(function()
- return datastore:GetAsync("user-"..player.userId)
- end)
- if(success)then --if we got it without a problem load data into game
- if data then --and we have saved data from the datastore
- print("loaded items from datastore: ")
- printTable(data)
- --set values in folder to the values that are in the datastore
- dataFolder.Coins.Value = data.Coins
- dataFolder.IsNewUser.Value = data.IsNewUser
- dataFolder.UsedCode.Value = data.UsedCode
- dataFolder.ClawboxTexture.Value = data.ClawboxTexture
- dataFolder.PayoutRate.Value = data.PayoutRate
- --set the bools for the items that we own in the closet to true
- for _, itemName in pairs(data.Closet)do
- local bool = dataFolder.Closet:FindFirstChild(itemName)
- bool.Value = true
- end
- else
- print("you are a new player so we don't need anything fron datastore")
- --do nothing, we already loaded the default data when we cloned the data folder from the replicated storage
- end
- else
- print("we couldnt get the data")
- end
- end)
- game.Players.PlayerRemoving:connect(function(player)
- local dataFolder = game.ReplicatedStorage.PlayerInformation[player.Name]
- local toSave = {
- Coins = dataFolder.Coins.Value,
- PayoutRate = dataFolder.PayoutRate.Value,
- IsNewUser = dataFolder.IsNewUser.Value,
- ClawboxTexture = dataFolder.ClawboxTexture.Value,
- UsedCode = dataFolder.UsedCode.Value,
- }
- --save all the owned items into a table and then save that table into the toSave table
- local closet = {}
- for i, v in pairs(dataFolder.Closet:GetChildren())do
- if(v.Value == true)then
- table.insert(closet, v.Name)
- end
- end
- toSave["Closet"] = closet
- --this is for debugging
- print("saving the following to datastore: ")
- printTable(toSave)
- --attempt to save to datastore
- local success, err = pcall(function()
- datastore:SetAsync("user-" .. player.userId, toSave)
- end)
- dataFolder:Destroy() --remove the folder since we don't need it anymore
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement