Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ServerScriptService > Script
- local Players = game:GetService("Players")
- local Workspace = game:GetService("Workspace")
- local KEY_NAME = "Key"
- local function countPlayerKeys(player)
- local count = 0
- local backpack = player:FindFirstChildOfClass("Backpack")
- if backpack then
- for _, obj in ipairs(backpack:GetChildren()) do
- if obj:IsA("Tool") and obj.Name == KEY_NAME then
- count += 1
- end
- end
- end
- local character = player.Character
- if character then
- for _, obj in ipairs(character:GetChildren()) do
- if obj:IsA("Tool") and obj.Name == KEY_NAME then
- count += 1
- end
- end
- end
- return count
- end
- local function getPlayerFromTool(tool)
- local parent = tool.Parent
- if not parent then
- return nil
- end
- if parent:IsA("Backpack") and parent.Parent and parent.Parent:IsA("Player") then
- return parent.Parent
- end
- if parent:IsA("Model") then
- return Players:GetPlayerFromCharacter(parent)
- end
- return nil
- end
- local function hookKey(tool)
- if not tool:IsA("Tool") then
- return
- end
- if tool.Name ~= KEY_NAME then
- return
- end
- if tool:GetAttribute("PickupHooked") then
- return
- end
- tool:SetAttribute("PickupHooked", true)
- tool.AncestryChanged:Connect(function()
- local player = getPlayerFromTool(tool)
- if not player then
- return
- end
- task.defer(function()
- if not tool.Parent then
- return
- end
- if countPlayerKeys(player) > 1 then
- tool:Destroy()
- end
- end)
- end)
- end
- for _, inst in ipairs(Workspace:GetDescendants()) do
- hookKey(inst)
- end
- Workspace.DescendantAdded:Connect(hookKey)
Advertisement
Add Comment
Please, Sign In to add comment