Advertisement
Azzz_4565

Untitled

Jun 27th, 2025
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. -- HYBRID Ultra-Performance Instant Respawn System
  2. -- Combining speed optimization with bulletproof reliability
  3.  
  4. local Players = game:GetService("Players")
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local RunService = game:GetService("RunService")
  7. local LocalPlayer = Players.LocalPlayer
  8.  
  9. -- Performance optimizations
  10. local taskSpawn = task.spawn
  11. local taskWait = task.wait
  12. local tick = tick
  13.  
  14. -- Advanced state management
  15. local RespawnState = {
  16.     isRespawning = false,
  17.     lastDeath = 0,
  18.     connectionId = 0,
  19.     guideRemote = nil
  20. }
  21.  
  22. -- Connection management
  23. local ConnectionCache = {}
  24.  
  25. -- Pre-cache Guide remote for maximum speed
  26. local function CacheGuideRemote()
  27.     if not RespawnState.guideRemote then
  28.         RespawnState.guideRemote = ReplicatedStorage:FindFirstChild("Guide")
  29.         if not RespawnState.guideRemote then
  30.             taskSpawn(function()
  31.                 RespawnState.guideRemote = ReplicatedStorage:WaitForChild("Guide", 10)
  32.             end)
  33.         end
  34.     end
  35. end
  36.  
  37. -- Ultra-optimized respawn function with smart fallbacks
  38. local function InstantRespawn()
  39.     local currentTime = tick()
  40.    
  41.     -- Prevent spam calls (frame-perfect protection)
  42.     if RespawnState.isRespawning or (currentTime - RespawnState.lastDeath) < 0.05 then
  43.         return
  44.     end
  45.    
  46.     RespawnState.isRespawning = true
  47.     RespawnState.lastDeath = currentTime
  48.    
  49.     taskSpawn(function()
  50.         -- Primary method: Cached Guide remote (fastest)
  51.         if RespawnState.guideRemote and RespawnState.guideRemote:IsA("RemoteEvent") then
  52.             RespawnState.guideRemote:FireServer()
  53.         else
  54.             -- Fallback: Search for Guide remote
  55.             local guide = ReplicatedStorage:FindFirstChild("Guide")
  56.             if guide and guide:IsA("RemoteEvent") then
  57.                 guide:FireServer()
  58.                 RespawnState.guideRemote = guide -- Cache it
  59.             else
  60.                 -- Emergency fallback: LoadCharacter
  61.                 pcall(function()
  62.                     LocalPlayer:LoadCharacter()
  63.                 end)
  64.             end
  65.         end
  66.        
  67.         -- Reset respawn flag
  68.         taskWait(0.05)
  69.         RespawnState.isRespawning = false
  70.     end)
  71. end
  72.  
  73. -- Hybrid connection system (multiple detection + single management)
  74. local function SetupHybridConnections(character)
  75.     if not character then return end
  76.    
  77.     -- Increment connection ID for bulletproof tracking
  78.     RespawnState.connectionId = RespawnState.connectionId + 1
  79.     local connectionId = RespawnState.connectionId
  80.    
  81.     -- Clean previous connections
  82.     if ConnectionCache[character] then
  83.         for _, conn in pairs(ConnectionCache[character]) do
  84.             if conn and conn.Connected then
  85.                 conn:Disconnect()
  86.             end
  87.         end
  88.     end
  89.     ConnectionCache[character] = {}
  90.    
  91.     local function setupConnections()
  92.         local humanoid = character:FindFirstChildWhichIsA("Humanoid")
  93.         if humanoid then
  94.             -- Primary: Died event (most reliable)
  95.             local deathConn = humanoid.Died:Connect(function()
  96.                 if RespawnState.connectionId == connectionId then
  97.                     InstantRespawn()
  98.                 end
  99.             end)
  100.            
  101.             -- Secondary: Health monitoring (backup detection)
  102.             local healthConn = humanoid.HealthChanged:Connect(function(health)
  103.                 if health <= 0 and RespawnState.connectionId == connectionId and not RespawnState.isRespawning then
  104.                     InstantRespawn()
  105.                 end
  106.             end)
  107.            
  108.             ConnectionCache[character] = {deathConn, healthConn}
  109.             return true
  110.         end
  111.         return false
  112.     end
  113.    
  114.     -- Immediate setup or wait for humanoid
  115.     if not setupConnections() then
  116.         local attempts = 0
  117.         local setupLoop
  118.         setupLoop = RunService.Heartbeat:Connect(function()
  119.             attempts = attempts + 1
  120.             if setupConnections() or attempts > 150 then -- 2.5 second timeout
  121.                 setupLoop:Disconnect()
  122.             end
  123.         end)
  124.     end
  125. end
  126.  
  127. -- Streamlined character monitoring
  128. local function OnCharacterAdded(character)
  129.     -- Cache Guide remote on first character spawn
  130.     CacheGuideRemote()
  131.    
  132.     -- Wait for essential components
  133.     character:WaitForChild("HumanoidRootPart", 5)
  134.    
  135.     -- Minimal delay for stability
  136.     taskWait()
  137.    
  138.     -- Setup hybrid connections
  139.     SetupHybridConnections(character)
  140. end
  141.  
  142. -- Lightweight watchdog (reduced overhead)
  143. local function CreateLightWatchdog()
  144.     taskSpawn(function()
  145.         while true do
  146.             taskWait(1) -- Check every second instead of every frame
  147.            
  148.             -- Verify current character has connections
  149.             if LocalPlayer.Character then
  150.                 local character = LocalPlayer.Character
  151.                 if not ConnectionCache[character] or #ConnectionCache[character] == 0 then
  152.                     SetupHybridConnections(character)
  153.                 end
  154.             end
  155.            
  156.             -- Re-cache Guide remote if lost
  157.             if not RespawnState.guideRemote then
  158.                 CacheGuideRemote()
  159.             end
  160.         end
  161.     end)
  162. end
  163.  
  164. -- Initialize system
  165. LocalPlayer.CharacterAdded:Connect(OnCharacterAdded)
  166.  
  167. -- Handle current character
  168. if LocalPlayer.Character then
  169.     OnCharacterAdded(LocalPlayer.Character)
  170. end
  171.  
  172. -- Cleanup on character removal
  173. LocalPlayer.CharacterRemoving:Connect(function(character)
  174.     if ConnectionCache[character] then
  175.         for _, conn in pairs(ConnectionCache[character]) do
  176.             if conn and conn.Connected then
  177.                 conn:Disconnect()
  178.             end
  179.         end
  180.         ConnectionCache[character] = nil
  181.     end
  182.     RespawnState.isRespawning = false
  183. end)
  184.  
  185. -- Start lightweight monitoring
  186. CreateLightWatchdog()
  187.  
  188. -- Global emergency respawn function
  189. _G.ForceRespawn = InstantRespawn
  190.  
  191. print("HYBRID Ultra-Performance Instant Respawn System Active - Maximum Speed + Reliability")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement