Advertisement
DrawingJhon

Protect Accessories

May 17th, 2024
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2.  
  3. local function handlePlayer(player)
  4.     NLS([==[script:Destroy()
  5.     local Players = game:GetService("Players")
  6.     local userId = ]==]..owner.UserId..[==[
  7.  
  8.     local function protectAccessory(accessory, character, humanoid)
  9.         if not accessory:IsA("Accoutrement") or accessory:FindFirstChild("ClientClone") then return end
  10.  
  11.         local template = accessory:Clone()
  12.  
  13.         local connection
  14.         connection = accessory.AncestryChanged:Connect(function()
  15.             if not accessory:IsDescendantOf(character) then
  16.                 connection:Disconnect()
  17.  
  18.                 local clone = template:Clone()
  19.                 Instance.new("BoolValue", clone).Name = "ClientClone"
  20.                 humanoid:AddAccessory(clone)
  21.  
  22.                 accessory:Destroy()
  23.             end
  24.         end)
  25.     end
  26.  
  27.     local function protectCharacter(character)
  28.         local humanoid = character:WaitForChild("Humanoid")
  29.  
  30.         for _, accessory in pairs(character:GetDescendants()) do
  31.             task.spawn(protectAccessory, accessory, character, humanoid)
  32.         end
  33.  
  34.         character.DescendantAdded:Connect(function(accessory)
  35.             protectAccessory(accessory, character, humanoid)
  36.         end)
  37.     end
  38.  
  39.     local function protectPlayer(player)
  40.         if player.UserId ~= userId then return end
  41.  
  42.         if player.Character then
  43.             task.spawn(protectCharacter, player.Character)
  44.         end
  45.  
  46.         player.CharacterAdded:Connect(protectCharacter)
  47.     end
  48.  
  49.     for _, player in pairs(Players:GetPlayers()) do
  50.         task.spawn(protectPlayer, player)
  51.     end
  52.  
  53.     Players.PlayerAdded:Connect(protectPlayer)
  54.     ]==], player.PlayerGui)
  55. end
  56.  
  57. for _, player in Players:GetPlayers() do
  58.     task.spawn(handlePlayer, player)
  59. end
  60.  
  61. Players.PlayerAdded:Connect(handlePlayer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement