Advertisement
DrawingJhon

Jail Script

Feb 17th, 2023 (edited)
1,558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. local JailModel = workspace.JAIL
  2. local JailCFrame = JailModel.JAILPART.CFrame
  3. local JailSize = JailModel.JAILPART.Size
  4.  
  5. local JailBackup = JailModel:Clone()
  6.  
  7. local Players = game:GetService("Players")
  8.  
  9. function startup()
  10.     local connection1, connection2, connection3 = nil, nil, nil
  11.  
  12.     local ignoreList = {}
  13.  
  14.     local function reset()
  15.         connection1:Disconnect()
  16.         connection2:Disconnect()
  17.         connection3:Disconnect()
  18.        
  19.         pcall(function()
  20.             JailModel:Destroy()
  21.         end)
  22.  
  23.         local clone = JailBackup:Clone()
  24.  
  25.         JailModel = clone
  26.         JailCFrame = clone.JAILPART.CFrame
  27.         JailSize = clone.JAILPART.Size
  28.        
  29.         clone.Parent = workspace
  30.  
  31.         startup()
  32.     end
  33.  
  34.     connection1 = JailModel.DescendantRemoving:Connect(function(child)
  35.         if ignoreList[child] then
  36.             ignoreList[child] = nil
  37.             return
  38.         end
  39.         reset()
  40.     end)
  41.     connection2 = JailModel:GetPropertyChangedSignal("Parent"):Connect(reset)
  42.     connection3 = JailModel.DescendantAdded:Connect(function(child)
  43.         ignoreList[child] = true
  44.     end)
  45. end
  46.  
  47. startup()
  48.  
  49. local jailCommand = ".jail "
  50. local unjailCommand = ".unjail "
  51.  
  52. function getPlayers(name)
  53.     local players = {}
  54.  
  55.     if name == "all" then
  56.         return Players:GetPlayers()
  57.     elseif name == "me" then
  58.         return {Players:GetPlayerByUserId(owner.UserId)}
  59.     elseif name ~= "" then
  60.         local usedDisplay = false
  61.         for i, v in pairs(Players:GetPlayers()) do
  62.             if v.DisplayName:sub(1, #name):lower() == name:lower() then
  63.                 usedDisplay = true
  64.                 table.insert(players, v)
  65.             end
  66.         end
  67.         if not usedDisplay then
  68.             for i, v in pairs(Players:GetPlayers()) do
  69.                 if v.Name:sub(1, #name):lower() == name:lower() then
  70.                     table.insert(players, v)
  71.                 end
  72.             end
  73.         end
  74.     end
  75.     return players
  76. end
  77.  
  78. local InJailUsers = {}
  79.  
  80. function handleChat(message)
  81.     if message:lower():sub(1, #jailCommand) == jailCommand:lower() then
  82.         local name = message:sub(#jailCommand + 1)
  83.         for i, v in pairs(getPlayers(name)) do
  84.             print("Jailed "..v.Name)
  85.             InJailUsers[v.UserId] = true
  86.         end
  87.     end
  88.  
  89.     if message:lower():sub(1, #unjailCommand) == unjailCommand:lower() then
  90.         local name = message:sub(#unjailCommand + 1)
  91.         for i, v in pairs(getPlayers(name)) do
  92.             print("Unjailed "..v.Name)
  93.             local wasJailed = InJailUsers[v.UserId]
  94.             InJailUsers[v.UserId] = nil
  95.             if wasJailed then
  96.                 v:LoadCharacter()
  97.             end
  98.         end
  99.     end
  100. end
  101.  
  102. owner.Chatted:Connect(handleChat)
  103.  
  104. Players.PlayerAdded:Connect(function(player)
  105.     if player.UserId == owner.UserId then
  106.         player.Chatted:Connect(handleChat)
  107.     end
  108. end)
  109.  
  110. game:GetService("RunService").Heartbeat:Connect(function()
  111.     local parts = workspace:GetPartBoundsInBox(JailCFrame, JailSize)
  112.  
  113.     for _, player in pairs(Players:GetPlayers()) do
  114.         if not InJailUsers[player.UserId] then continue end
  115.         task.spawn(function()
  116.             local character = player.Character
  117.             if not character then return end
  118.             local head = character:FindFirstChild("Head")
  119.             if not head or not head:IsA("BasePart") then return end
  120.  
  121.             if not table.find(parts, head) then
  122.                 character:PivotTo(JailCFrame)
  123.             end
  124.         end)
  125.     end
  126.  
  127.     for _, part in pairs(JailModel:GetChildren()) do
  128.         if part:IsA("BasePart") then
  129.             part.Locked = true
  130.         end
  131.     end
  132.  
  133.     pcall(function() JailModel.JAILPART.CanCollide = false end)
  134. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement