HowToRoblox

ShopServer

Dec 6th, 2021 (edited)
915
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local items = game.ReplicatedStorage:WaitForChild("ShopItems")
  2. local re = game.ReplicatedStorage:WaitForChild("ShopRE")
  3.  
  4. local itemNumber = 3
  5.  
  6.  
  7. local dss = game:GetService("DataStoreService")
  8. local shopDS = dss:GetDataStore("DSShop")
  9.  
  10.  
  11. game.Players.PlayerAdded:Connect(function(p)
  12.  
  13.     local ls = Instance.new("Folder", p)
  14.     ls.Name = "leaderstats"
  15.  
  16.     local cash = Instance.new("IntValue", ls)
  17.     cash.Name = "Cash"
  18.     cash.Value = 30000
  19.  
  20.  
  21.     local pData = shopDS:GetAsync(p.UserId) or {30000}
  22.  
  23.     cash.Value = pData[1]
  24.  
  25.     table.remove(pData, 1)
  26.  
  27.     for i, data in pairs(pData) do
  28.  
  29.         items[data]:Clone().Parent = p.StarterGear
  30.         items[data]:Clone().Parent = p.Backpack
  31.     end
  32. end)
  33.  
  34.  
  35. local currentTime = os.time()
  36. local previousTime = shopDS:GetAsync("LastShopUpdate") or {0}
  37.  
  38.  
  39. local function createShop()
  40.    
  41.     game.ReplicatedStorage.DailyShop:ClearAllChildren()
  42.    
  43.     local newShop = {os.time()}
  44.  
  45.     local itemsToPick = items:GetChildren()
  46.  
  47.     while #newShop < itemNumber + 1 do
  48.  
  49.         local newItem = itemsToPick[math.random(1, #itemsToPick)]
  50.        
  51.         newItem:Clone().Parent = game.ReplicatedStorage.DailyShop
  52.         table.insert(newShop, newItem.Name)
  53.         table.remove(itemsToPick, table.find(itemsToPick, newItem))
  54.     end
  55.  
  56.     shopDS:SetAsync("LastShopUpdate", newShop)
  57. end
  58.  
  59.  
  60. local function saveData(p)
  61.    
  62.     local items = {p.leaderstats.Cash.Value}
  63.     for i, c in pairs(p.StarterGear:GetChildren()) do
  64.         table.insert(items, c.Name)
  65.     end
  66.    
  67.     shopDS:SetAsync(p.UserId, items)
  68. end
  69.  
  70.  
  71. game.Players.PlayerRemoving:Connect(saveData)
  72.  
  73. game:BindToClose(function()
  74.     for i, p in pairs(game.Players:GetPlayers()) do
  75.         saveData(p)
  76.     end
  77. end)
  78.  
  79.  
  80.  
  81. re.OnServerEvent:Connect(function(p, item)
  82.  
  83.     if item and item.Parent == game.ReplicatedStorage.DailyShop and p.leaderstats.Cash.Value >= item.Price.Value then
  84.  
  85.         p.leaderstats.Cash.Value -= item.Price.Value
  86.  
  87.         item:Clone().Parent = p.Backpack
  88.         item:Clone().Parent = p.StarterGear
  89.     end
  90. end)
  91.  
  92.  
  93.  
  94. if currentTime - previousTime[1] >= 24*60*60 then
  95.    
  96.     createShop()
  97.    
  98.    
  99. else
  100.    
  101.     for i = 2, #previousTime do
  102.        
  103.         items[previousTime[i]]:Clone().Parent = game.ReplicatedStorage.DailyShop
  104.     end
  105. end
  106.  
  107.  
  108. while wait(1) do
  109.  
  110.     local previousTime = shopDS:GetAsync("LastShopUpdate")
  111.  
  112.     local timeToReset = (24 * 60 * 60 ) - (os.time() - previousTime[1])
  113.     local hrs = math.floor(timeToReset / 60 / 60)
  114.     local mins = string.format("%0.2i", math.floor(timeToReset / 60) - (hrs * 60))
  115.     local secs = string.format("%0.2i", timeToReset - (hrs * 60 * 60) - (mins * 60))
  116.  
  117.     game.ReplicatedStorage.TimeForReset.Value = hrs .. ":" .. mins .. ":" .. secs
  118.  
  119.     if timeToReset <= 0 then
  120.  
  121.         createShop()
  122.     end
  123. end
RAW Paste Data