HowToRoblox

TrailShopHandler

Jun 9th, 2021 (edited)
361
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local shopBtn = script.Parent:WaitForChild("OpenShop")
  2. local invBtn = script.Parent:WaitForChild("OpenInv")
  3.  
  4. local shopFrame = script.Parent:WaitForChild("ShopFrame")
  5. local invFrame = script.Parent:WaitForChild("InventoryFrame")
  6.  
  7. shopFrame.Visible, invFrame.Visible = false, false
  8.  
  9.  
  10. shopBtn.MouseButton1Click:Connect(function()
  11.     shopFrame.Visible = not shopFrame.Visible; invFrame.Visible = false
  12. end)
  13. invBtn.MouseButton1Click:Connect(function()
  14.     invFrame.Visible = not invFrame.Visible; shopFrame.Visible = false
  15. end)
  16.  
  17.  
  18. shopFrame.CloseButton.MouseButton1Click:Connect(function()
  19.     shopFrame.Visible = false
  20. end)
  21. invFrame.CloseButton.MouseButton1Click:Connect(function()
  22.     invFrame.Visible = false
  23. end)
  24.  
  25.  
  26. local trailsFolder = game.ReplicatedStorage:WaitForChild("Trails")
  27. local ownedTrailsFolder = game.Players.LocalPlayer:WaitForChild("OwnedTrails")
  28.  
  29.  
  30. function updateInventory()
  31.    
  32.     for i, child in pairs(invFrame.TrailsScroller:GetChildren()) do
  33.        
  34.         if child:IsA("ImageLabel") then child:Destroy() end
  35.     end
  36.    
  37.    
  38.     local ownedTrails = ownedTrailsFolder:GetChildren()
  39.    
  40.     table.sort(ownedTrails, function(a, b)
  41.         return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
  42.     end)
  43.    
  44.    
  45.     for i, trail in pairs(ownedTrails) do
  46.        
  47.         local item = script.Item:Clone()
  48.         item.SelectButton.ButtonText.Text = "EQUIP"
  49.         item.TrailName.Text = trail.Name
  50.         item.Trail.UIGradient.Color = trail.Color
  51.        
  52.         item.Parent = invFrame.TrailsScroller
  53.        
  54.        
  55.         if game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild(trail.Name) then
  56.             item.SelectButton.ButtonText.Text = "EQUIPPED"
  57.         end
  58.        
  59.        
  60.         item.SelectButton.MouseButton1Click:Connect(function()
  61.             game.ReplicatedStorage.TrailSelectedRE:FireServer(false, trail)
  62.            
  63.             for i, itemButton in pairs(item.Parent:GetChildren()) do
  64.                
  65.                 if itemButton:IsA("ImageLabel") then
  66.                     itemButton.SelectButton.ButtonText.Text = "EQUIP"
  67.                 end
  68.             end
  69.             item.SelectButton.ButtonText.Text = "EQUIPPED"
  70.         end)
  71.     end
  72. end
  73.  
  74.  
  75. function updateShop()
  76.  
  77.     for i, child in pairs(shopFrame.TrailsScroller:GetChildren()) do
  78.  
  79.         if child:IsA("ImageLabel") then child:Destroy() end
  80.     end
  81.  
  82.  
  83.     local shopTrails = trailsFolder:GetChildren()
  84.  
  85.     table.sort(shopTrails, function(a, b)
  86.         return a.Price.Value < b.Price.Value or a.Price.Value == b.Price.Value and a.Name < b.Name
  87.     end)
  88.  
  89.  
  90.     for i, trail in pairs(shopTrails) do
  91.  
  92.         local item = script.Item:Clone()
  93.        
  94.         item.SelectButton.ButtonText.Text = "BUY for " .. trail.Price.Value
  95.         if ownedTrailsFolder:FindFirstChild(trail.Name) then item.SelectButton.ButtonText.Text = "Already bought." end
  96.        
  97.         item.TrailName.Text = trail.Name
  98.         item.Trail.UIGradient.Color = trail.Color
  99.  
  100.         item.Parent = shopFrame.TrailsScroller
  101.  
  102.  
  103.         item.SelectButton.MouseButton1Click:Connect(function()
  104.             game.ReplicatedStorage.TrailSelectedRE:FireServer(true, trail)
  105.         end)
  106.     end
  107. end
  108.  
  109. updateShop()
  110. updateInventory()
  111.  
  112. ownedTrailsFolder.ChildAdded:Connect(function()
  113.     updateInventory(); updateShop()
  114. end)
  115. ownedTrailsFolder.ChildRemoved:Connect(function()
  116.     updateInventory(); updateShop()
  117. end)
  118.  
  119. trailsFolder.ChildAdded:Connect(updateShop)
  120. trailsFolder.ChildRemoved:Connect(updateShop)
RAW Paste Data