Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DEFAULT_VALUE = 5 -- Starting value of credits
- local UNSUCCESSFUL_RETRY_TIME = 5 -- Retry time in case of unsuccessful credit save
- local CreditCooldown = 5
- local IncomeIncrease = 5
- local Credit = { }
- local QueuedPlayers = { }
- local CreditText = game.StarterGui.Credits.Credits.TextLabel
- local DataStoreService = game:GetService("DataStoreService")
- local CreditData = DataStoreService:GetDataStore("StitchDreamzIsCool")
- local RS = game:GetService("RunService")
- Credit.SetupPlayer = function(Player)
- local Data = Instance.new("Folder")
- Data.Name = "Data"
- Data.Parent = Player
- local Successful, Save = Credit:LocateSave(Player)
- local Credits = Instance.new("IntValue")
- Credits.Name = "CREDITS"
- Credits.Parent = Data
- Credits.Value = Save or DEFAULT_VALUE
- CreditText.Text = "Credits: " .. Credits.Value
- while Player do
- wait(CreditCooldown)
- if Player ~= nil and Player:IsDescendantOf(game.Players) then
- Credits.Value += IncomeIncrease
- CreditText.Text = "Credits: " .. Credits.Value
- end
- end
- end
- function Credit:LocateSave(Player)
- local UserId = Player.UserId
- assert(type(UserId) == "number", "Credit:LocateSave() failed! (Invalid UserId)")
- local DataReturn = CreditData:GetAsync(UserId)
- return DataReturn ~= nil, DataReturn
- end
- function Credit:SavePlayer(Player)
- if RS:IsStudio() then
- return
- end
- local UserId = Player.UserId
- -- assert(type(UserId) == "number", "Credit:LocateSave() failed! (Invalid UserId)")
- local Data = Player:FindFirstChild("DATA"):FindFirstChild("CREDITS")
- if not Data then
- Data = { }
- Data["Value"] = DEFAULT_VALUE
- end
- local Successful = pcall(function()
- CreditData:SetAsync(UserId, Data.Value)
- end)
- if not Successful then
- warn("[DATASTORE] UNSUCCESSFUL SAVE OF ".. Player.Name.. "! RETRYING")
- delay(UNSUCCESSFUL_RETRY_TIME, 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
- Credit:SavePlayer(Player)
- end
- end
- return Credit
Advertisement
Add Comment
Please, Sign In to add comment