Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dataService = {}
- local DataStoreService = game:GetService("DataStoreService")
- local ServerStorage = game:GetService("ServerStorage")
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local dataStorage = DataStoreService:GetDataStore("PlayerData")
- function setDataAsync(player)
- local playerDataFolder = ServerStorage.PlayerData[Players:GetNameFromUserIdAsync(player.UserId)]
- local cash = playerDataFolder.Cash
- local gameTime = playerDataFolder.Time
- local success, errorMessage = pcall(function()
- dataStorage:SetAsync(player.UserId .. " Cash", cash.Value)
- dataStorage:SetAsync(player.UserId .. " Time", gameTime.Value)
- end)
- if errorMessage then
- warn(errorMessage)
- end
- end
- function dataService.Init()
- Players.PlayerRemoving:Connect(setDataAsync)
- game:BindToClose(function()
- for i, player in pairs(game.Players:GetPlayers()) do
- setDataAsync(player)
- end
- end)
- Players.PlayerAdded:Connect(function(player)
- local playerFolder = Instance.new("Folder")
- playerFolder.Name = player.Name
- playerFolder.Parent = ServerStorage:WaitForChild("PlayerData")
- local cash = Instance.new("IntValue")
- cash.Name = "Cash"
- cash.Parent = playerFolder
- cash.Value = 50
- local gameTime = Instance.new("IntValue")
- gameTime.Name = "Time"
- gameTime.Parent = playerFolder
- gameTime.Value = 0
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- local cashLS = Instance.new("IntValue")
- cashLS.Name = "Cash"
- cashLS.Parent = leaderstats
- local cashData = nil
- local timeData = nil
- local success, errorMessage = pcall(function()
- cashData = dataStorage:GetAsync(player.UserId .. " Cash")
- timeData = dataStorage:GetAsync(player.UserId .. " Time")
- end)
- if errorMessage then
- player:Kick("There has been an issue with loading your Data, please rejoin after a few minutes.")
- end
- cash.Value = cashData or 0
- cashLS.Value = cash.Value
- gameTime.Value = timeData or 0
- while wait(1) do
- gameTime.Value += 1
- end
- end)
- end
- return dataService
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement