Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStoreService = game:GetService("DataStoreService")
- local CreditDatastore = DataStoreService:GetDataStore("DevelopmentCreditsTest")
- local configfile = require(script.configData)
- local dataConfig = configfile.dataConfig
- local Credit = { }
- local QueuedPlayers = { }
- local RunService = game:GetService("RunService")
- Credit.SetupPlayer = function(Player)
- local Data = Instance.new("Folder")
- Data.Name = "Data"
- print(Player)
- Data.Parent = Player.UserId
- local Successful, Save = Credit:LocateSave(Player)
- local Credits = Instance.new("IntValue")
- Credits.Name = "Credits"
- Credits.Parent = Data
- Credits.Value = Save or dataConfig.StarterCash
- while Player do
- wait(dataConfig.CreditCooldown)
- if Player ~= nil and Player:IsDescendantOf(game.Players) then
- Credits.Value += dataConfig.IncomeIncrease
- end
- end
- end
- function Credit:LocateSave(Player)
- local UserId = Player.UserId
- print(UserId)
- assert(type(UserId) == "number", "Credit:LocateSave() failed! (Invalid UserId)")
- local DataReturn = CreditDatastore:GetAsync(UserId)
- return DataReturn ~= nil, DataReturn
- end
- function Credit:SavePlayer(Player)
- if RunService:IsStudio() then
- return
- end
- local UserId = Player.UserId
- print(UserId)
- assert(type(UserId) == "number", "Credit:LocateSave() failed! (Invalid UserId)")
- local Data = Player:FindFirstChild("Data"):FindFirstChild("Credits")
- if not Data then
- Data = { }
- Data["Value"] = dataConfig.StarterCash
- end
- local Successful = pcall(function()
- CreditDatastore:SetAsync(UserId, Data.Value)
- end)
- if not Successful then
- warn("[DATASTORE] UNSUCCESSFUL SAVE OF ".. Player.Name.. "! RETRYING")
- delay(dataConfig.RetryTime, function()
- self:SavePlayer(Player)
- end)
- else
- print("[DATASTORE] SUCCESSFUL SAVE OF ".. Player.Name)
- end
- end
- Credit.SaveAllPlayers = function()
- for _, Player in next, game.Players:GetPlayers() do
- local Credits = Player:FindFirstChild("Data"):FindFirstChild("Credits").Value
- Credit:SavePlayer(Player)
- end
- end
- return Credit
Advertisement
Add Comment
Please, Sign In to add comment