local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Http = game:GetService("HttpService")
local remoteEvent = ReplicatedStorage:WaitForChild("ToolClaimEvent")
local lootsFolder = ServerStorage:WaitForChild("Loots")
local Drawers = workspace.Drawers
local hasLoot = {}
local usedLootDrawers = {}
local function handleLootItems(part)
local claimedOnes = {}
local toolGUID = Http:GenerateGUID()
local detect = Instance.new("ClickDetector")
detect.MaxActivationDistance = 32
detect.Parent = part
detect.MouseClick:Connect(function(player: Player)
local tool = game:GetService("ServerStorage"):WaitForChild("Items"):FindFirstChild(part.Name) -- Checks if theres a tool with the same name as the clicking part
if not tool then return end
local playerBackpack = player:WaitForChild("Backpack")
if not claimedOnes[player.UserId] then claimedOnes[player.UserId] = {} end
if not claimedOnes[player.UserId][toolGUID] then
tool:Clone().Parent = playerBackpack
remoteEvent:FireClient(player,part)
claimedOnes[player.UserId][toolGUID] = true
end
end)
end
local function spawnLoots(lootTools)
for i, cloned in lootTools:GetChildren() do
cloned = cloned:Clone()
local randomLootDrawer = Drawers:GetChildren()[math.random(#Drawers:GetChildren())]
while usedLootDrawers[randomLootDrawer] do
randomLootDrawer = Drawers:GetChildren()[math.random(#Drawers:GetChildren())]
end
local randomDrawer = randomLootDrawer:GetChildren()[math.random(#randomLootDrawer:GetChildren())]
while not randomDrawer:IsA("Model") or hasLoot[randomDrawer] do
randomDrawer = randomLootDrawer:GetChildren()[math.random(#randomLootDrawer:GetChildren())]
end
hasLoot[randomDrawer] = true
cloned:PivotTo(randomDrawer:GetPivot() * CFrame.Angles(0, 0, math.rad(-90)))
cloned.Parent = randomDrawer
handleLootItems(cloned)
end
end
spawnLoots(lootsFolder:GetChildren()[math.random(#lootsFolder:GetChildren())])