Advertisement
Sungmingamerpro13

RandomLootSpawn

Oct 22nd, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 2.02 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local ServerStorage = game:GetService("ServerStorage")
  3. local Http = game:GetService("HttpService")
  4.  
  5. local remoteEvent = ReplicatedStorage:WaitForChild("ToolClaimEvent")
  6. local lootsFolder = ServerStorage:WaitForChild("Loots")
  7.  
  8. local Drawers = workspace.Drawers
  9.  
  10. local hasLoot = {}
  11. local usedLootDrawers = {}
  12.  
  13. local function handleLootItems(part)
  14.     local claimedOnes = {}
  15.  
  16.     local toolGUID = Http:GenerateGUID()
  17.  
  18.     local detect = Instance.new("ClickDetector")
  19.     detect.MaxActivationDistance = 32
  20.     detect.Parent = part
  21.  
  22.     detect.MouseClick:Connect(function(player: Player)
  23.         local tool = game:GetService("ServerStorage"):WaitForChild("Items"):FindFirstChild(part.Name) -- Checks if theres a tool with the same name as the clicking part
  24.         if not tool then return end
  25.         local playerBackpack = player:WaitForChild("Backpack")
  26.  
  27.         if not claimedOnes[player.UserId] then claimedOnes[player.UserId] = {} end
  28.         if not claimedOnes[player.UserId][toolGUID] then
  29.             tool:Clone().Parent = playerBackpack
  30.             remoteEvent:FireClient(player,part)
  31.             claimedOnes[player.UserId][toolGUID] = true
  32.         end
  33.     end)
  34. end
  35.  
  36. local function spawnLoots(lootTools)
  37.     for i, cloned in lootTools:GetChildren() do
  38.         cloned = cloned:Clone()
  39.        
  40.         local randomLootDrawer = Drawers:GetChildren()[math.random(#Drawers:GetChildren())]
  41.        
  42.         while usedLootDrawers[randomLootDrawer] do
  43.             randomLootDrawer = Drawers:GetChildren()[math.random(#Drawers:GetChildren())]
  44.         end
  45.        
  46.         local randomDrawer = randomLootDrawer:GetChildren()[math.random(#randomLootDrawer:GetChildren())]
  47.  
  48.         while not randomDrawer:IsA("Model") or hasLoot[randomDrawer] do
  49.             randomDrawer = randomLootDrawer:GetChildren()[math.random(#randomLootDrawer:GetChildren())]
  50.         end
  51.  
  52.         hasLoot[randomDrawer] = true
  53.  
  54.         cloned:PivotTo(randomDrawer:GetPivot() * CFrame.Angles(0, 0, math.rad(-90)))
  55.         cloned.Parent = randomDrawer
  56.  
  57.         handleLootItems(cloned)
  58.     end
  59. end
  60.  
  61. spawnLoots(lootsFolder:GetChildren()[math.random(#lootsFolder:GetChildren())])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement