Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStoreService = game:GetService("DataStoreService")
- local myDataStore = DataStoreService:GetDataStore("MyDataStore")
- game.Players.PlayerAdded:Connect(function(player)
- local leaderstats = Instance.new("Folder", player)
- leaderstats.Name = "leaderstats"
- local cash = Instance.new("IntValue", leaderstats)
- cash.Name = "Cash"
- local playerUserId = "Player_"..player.UserId
- -- Load data
- local data
- local success, errormessage = pcall(function()
- data = myDataStore:GetAsync(playerUserId)
- end)
- if success then
- cash.Value = data
- -- Set our data equal to the current cash
- end
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- local playerUserId = "Player_"..player.UserId
- local data = player.leaderstats.Cash.Value
- local success, errormessage = pcall(function()
- myDataStore:SetAsync(playerUserId, data)
- end)
- if success then
- print("Successfully saved!")
- else
- print("There was an error!")
- warn(errormessage)
- end
- myDataStore:SetAsync(playerUserId, data)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement