Hasli4

Untitled

Apr 19th, 2026
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. -- ServerScriptService > Script
  2.  
  3. local Players = game:GetService("Players")
  4. local Workspace = game:GetService("Workspace")
  5.  
  6. local KEY_NAME = "Key"
  7.  
  8. local function countPlayerKeys(player)
  9. local count = 0
  10.  
  11. local backpack = player:FindFirstChildOfClass("Backpack")
  12. if backpack then
  13. for _, obj in ipairs(backpack:GetChildren()) do
  14. if obj:IsA("Tool") and obj.Name == KEY_NAME then
  15. count += 1
  16. end
  17. end
  18. end
  19.  
  20. local character = player.Character
  21. if character then
  22. for _, obj in ipairs(character:GetChildren()) do
  23. if obj:IsA("Tool") and obj.Name == KEY_NAME then
  24. count += 1
  25. end
  26. end
  27. end
  28.  
  29. return count
  30. end
  31.  
  32. local function getPlayerFromTool(tool)
  33. local parent = tool.Parent
  34. if not parent then
  35. return nil
  36. end
  37.  
  38. if parent:IsA("Backpack") and parent.Parent and parent.Parent:IsA("Player") then
  39. return parent.Parent
  40. end
  41.  
  42. if parent:IsA("Model") then
  43. return Players:GetPlayerFromCharacter(parent)
  44. end
  45.  
  46. return nil
  47. end
  48.  
  49. local function hookKey(tool)
  50. if not tool:IsA("Tool") then
  51. return
  52. end
  53.  
  54. if tool.Name ~= KEY_NAME then
  55. return
  56. end
  57.  
  58. if tool:GetAttribute("PickupHooked") then
  59. return
  60. end
  61. tool:SetAttribute("PickupHooked", true)
  62.  
  63. tool.AncestryChanged:Connect(function()
  64. local player = getPlayerFromTool(tool)
  65. if not player then
  66. return
  67. end
  68.  
  69. task.defer(function()
  70. if not tool.Parent then
  71. return
  72. end
  73.  
  74. if countPlayerKeys(player) > 1 then
  75. tool:Destroy()
  76. end
  77. end)
  78. end)
  79. end
  80.  
  81. for _, inst in ipairs(Workspace:GetDescendants()) do
  82. hookKey(inst)
  83. end
  84.  
  85. Workspace.DescendantAdded:Connect(hookKey)
Advertisement
Add Comment
Please, Sign In to add comment