Advertisement
HowToRoblox

DailySpinServer

Sep 27th, 2022 (edited)
1,634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("DAILY SPINS DATA STORE")
  3.  
  4.  
  5. function save(plr)
  6.    
  7.     local cash = plr.leaderstats.Cash.Value
  8.     local lastSpun = plr.LastSpun.Value
  9.    
  10.     local combinedData = {cash, lastSpun}
  11.    
  12.     ds:SetAsync(plr.UserId, combinedData)
  13. end
  14.  
  15. game.Players.PlayerRemoving:Connect(save)
  16.  
  17. game:BindToClose(function()
  18.    
  19.     for i, plr in pairs(game.Players:GetPlayers()) do
  20.        
  21.         save(plr)
  22.     end
  23. end)
  24.  
  25.  
  26. game.Players.PlayerAdded:Connect(function(plr)
  27.    
  28.     local ls = Instance.new("Folder")
  29.     ls.Name = "leaderstats"
  30.     ls.Parent = plr
  31.    
  32.     local cashValue = Instance.new("IntValue")
  33.     cashValue.Name = "Cash"
  34.     cashValue.Parent = ls
  35.    
  36.     local lastSpunValue = Instance.new("IntValue")
  37.     lastSpunValue.Name = "LastSpun"
  38.     lastSpunValue.Parent = plr
  39.    
  40.    
  41.     local loadedData = ds:GetAsync(plr.UserId) or {0, 0}
  42.    
  43.     local cash = loadedData[1]
  44.     local lastSpun = loadedData[2]
  45.    
  46.     cashValue.Value = cash
  47.     lastSpunValue.Value = lastSpun
  48. end)
  49.  
  50.  
  51. local remoteEvent = game.ReplicatedStorage:WaitForChild("DailySpinReplicatedStorage"):WaitForChild("DailySpinRE")
  52.  
  53. local plrsSpinning = {}
  54.  
  55.  
  56. remoteEvent.OnServerEvent:Connect(function(plr)
  57.    
  58.     local lastSpunValue = plr.LastSpun
  59.     local timeSince = os.time() - lastSpunValue.Value
  60.    
  61.     if timeSince >= 24*60*60 and not plrsSpinning[plr] then
  62.         plrsSpinning[plr] = true
  63.        
  64.         local spinRewards = require(game.ReplicatedStorage.DailySpinReplicatedStorage:WaitForChild("DailySpinSettings")).rewards
  65.        
  66.         local rewardPos = math.random(1, #spinRewards)
  67.         local reward = spinRewards[rewardPos]
  68.        
  69.         local spinTime = Random.new():NextNumber(3, 5)
  70.        
  71.         remoteEvent:FireClient(plr, rewardPos, spinTime)
  72.        
  73.         task.wait(spinTime)
  74.        
  75.         lastSpunValue.Value = os.time()
  76.         plr.leaderstats.Cash.Value += reward
  77.        
  78.         plrsSpinning[plr] = false
  79.     end
  80. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement