Advertisement
Mr-Nightmare1

Dungeon Teleporter (Finished)

May 4th, 2024 (edited)
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.09 KB | Source Code | 0 0
  1. local Teleporter = {
  2.     __call = function(...)
  3.         return {...}
  4.     end,
  5. }
  6. Teleporter.__index = Teleporter
  7.  
  8. local players = game:GetService("Players")
  9. local teleportService = game:GetService("TeleportService")
  10. local replicatedStorage = game:GetService("ReplicatedStorage")
  11. local runService = game:GetService("RunService")
  12.  
  13. local packages = replicatedStorage.Packages
  14. local warpNetwork = require(packages.Warp)
  15.  
  16. local leaveButtonNetwork = warpNetwork.Server("LeaveButton")
  17.  
  18. function Teleporter.new(dungeonTeleporter: Model)
  19.    
  20.     local self = setmetatable({
  21.         _teleporter = dungeonTeleporter,
  22.         _placeId = game.PlaceId,
  23.         _playersTable = {},
  24.         _playerLimit = 4,
  25.         _activeConnections = {},
  26.         _deathConnections = {},
  27.         _teleporting = false
  28.     }, Teleporter)
  29.    
  30.     --[[
  31.     self.__newindex = function(newTable, index, value)
  32.         newTable[index] = value
  33.         return
  34.     end
  35.     --]]
  36.    
  37.     self:StartRegister()
  38.     return self
  39. end
  40.  
  41. function Teleporter:CleanDeathConnections()
  42.    
  43.     for _, connection: RBXScriptConnection in self._deathConnections do
  44.        
  45.         connection:Disconnect()
  46.     end
  47. end
  48.  
  49. function Teleporter:Teleport(actionText: TextLabel)
  50.     local playerTable = self._playersTable
  51.     local placeId = self._placeId
  52.    
  53.     for i = 5, 0, -1 do
  54.  
  55.         actionText.Text = "Teleporting in " .. i
  56.         if i == 1 then
  57.            
  58.             self:CleanDeathConnections()
  59.             local success, errorMessage = pcall(function()
  60.                
  61.                 local code = teleportService:ReserveServer(placeId)
  62.                 teleportService:TeleportToPrivateServer(placeId, code, playerTable)
  63.             end)
  64.  
  65.             if not (success) then
  66.                 print("----- Teleport Error -----")
  67.                 print(errorMessage)
  68.                 warn(errorMessage)
  69.                 print("----- Teleport Error -----")
  70.             end
  71.  
  72.             task.wait(4)
  73.             self._teleporting = false
  74.             self:Update(actionText)
  75.         end
  76.  
  77.         task.wait(1)
  78.     end
  79. end
  80.  
  81. function Teleporter:Update(actionText: TextLabel)
  82.    
  83.     actionText.Text = #self._playersTable.."/"..self._playerLimit
  84. end
  85.  
  86. function Teleporter:StartRegister()
  87.    
  88.     local model = self._teleporter
  89.     local teleportingZone = model.TeleportingZone
  90.     local teleportData = teleportingZone.TeleportData.Value
  91.     local aboveUi = teleportingZone.AboveUi
  92.     local frame = aboveUi.Frame
  93.     local actionText = frame.ActionText
  94.    
  95.     -- self._placeId = teleportingZone.PlaceId.Value
  96.  
  97.     local previousTime = tick()
  98.     local delayEnterTime = .175
  99.  
  100.     local touchBarrier: UnionOperation = model.TouchBarrier
  101.     self._activeConnections["Touch"] = touchBarrier.Touched:Connect(function(hit)
  102.        
  103.         local character = hit.Parent
  104.         local player = players:GetPlayerFromCharacter(character)
  105.         if player then
  106.            
  107.             if not (table.find(self._playersTable, player)) then
  108.                
  109.                 if self._teleporting == false and tick() - previousTime >= delayEnterTime and not (#self._playersTable >= self._playerLimit) then
  110.                     previousTime = tick()
  111.                    
  112.                     character:PivotTo(teleportingZone:GetPivot())
  113.                     table.insert(self._playersTable, player)
  114.                     self:Update(actionText)
  115.                    
  116.                     local humanoid = character.Humanoid
  117.                     local deathConnection = "playerDied".."_"..player.UserId
  118.                    
  119.                     leaveButtonNetwork:Fire(true, player, {action = "show"})
  120.                    
  121.                     self._deathConnections[deathConnection] = humanoid.Died:Connect(function()
  122.                        
  123.                         local found = table.find(self._playersTable, player)
  124.                         if found then
  125.  
  126.                             table.remove(self._playersTable, found)
  127.                             self._deathConnections[deathConnection]:Disconnect()
  128.                             self:Update(actionText)
  129.                         end
  130.                     end)
  131.                    
  132.                     local leaveConnection = leaveButtonNetwork:Connect(function(playerThatFired)
  133.                        
  134.                         if player == playerThatFired then
  135.                            
  136.                             self._deathConnections[deathConnection]:Disconnect()
  137.                             local found = table.find(self._playersTable, player)
  138.                             if found then
  139.                                 table.remove(self._playersTable, found)
  140.                             end
  141.                             if character then
  142.                                
  143.                                 character:PivotTo(model.Outside:GetPivot())
  144.                             end
  145.                            
  146.                             self:Update(actionText)
  147.                         end
  148.                     end)
  149.                 end
  150.             end
  151.         end
  152.     end)
  153.    
  154.     local check = nil
  155.     self._activeConnections["Heartbeat"] = runService.Heartbeat:Connect(function(deltaTime)
  156.        
  157.         if not (self._teleporting) then
  158.            
  159.             if #self._playersTable > 0 and not (check) then
  160.  
  161.                 check = tick()
  162.             end
  163.            
  164.             if #self._playersTable <= 0 and check then
  165.                
  166.                 check = nil
  167.             end
  168.  
  169.             if check then
  170.                 if tick() - check > 10 then
  171.  
  172.                     check = nil
  173.                     self._teleporting = true
  174.                    
  175.                     for _, player in self._playersTable do
  176.                        
  177.                         leaveButtonNetwork:Fire(true, player, {action = "hide"})
  178.                     end
  179.                    
  180.                     self:Teleport(actionText)
  181.                 end
  182.             end
  183.         end
  184.     end)
  185.    
  186.     self._activeConnections["PlayerRemoving"] = players.PlayerRemoving:Connect(function(player)
  187.        
  188.         local found = table.find(self._playersTable, player)
  189.         if found then
  190.            
  191.             table.remove(self._playersTable, found)
  192.             self:Update(actionText)
  193.         end
  194.     end)
  195.    
  196.     local destroyConnection
  197.     destroyConnection = model.Destroying:Connect(function()
  198.        
  199.         for _, connection: RBXScriptConnection in self._activeConnections do
  200.            
  201.             connection:Disconnect()
  202.             destroyConnection:Disconnect()
  203.         end
  204.     end)
  205. end
  206.  
  207. return Teleporter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement