Advertisement
qsenko1

Spectate System

Apr 24th, 2024
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.26 KB | None | 0 0
  1. local Player = game.Players.LocalPlayer
  2. local RunService = game:GetService("RunService")
  3. local CoreGui = game:GetService("StarterGui")
  4. local Character = Player.Character or Player.CharacterAdded:wait()
  5. local Humanoid = Character:WaitForChild("Humanoid")
  6. local UI = script.Parent--Player.PlayerGui:WaitForChild("SpectateUI")
  7. local SpectateButton = UI.SpectateButton
  8. local SpectateFrame = UI.SpectateFrame
  9. local PreviousButton = SpectateFrame.BackRoundImage.PreviousButton
  10. local ClickSound = Player.PlayerGui:WaitForChild("LocalSounds")["Clicked Sound"]
  11. local ShopButton = Player.PlayerGui:WaitForChild("ShopUI").OpenShopButton
  12. local CodesButton = Player.PlayerGui:WaitForChild("CodesUI").OpenShopButton
  13. local StatsButton = Player.PlayerGui:WaitForChild("StatsGui").StatsButton
  14. local CoinsShopButton = Player.PlayerGui:WaitForChild("Coins&GemsShop").CoinsShopButton
  15. local GemsShopButton = Player.PlayerGui:WaitForChild("Coins&GemsShop").GemsShopButton
  16. local DieButton = Player.PlayerGui:WaitForChild("ButtonGui").Die
  17. local LobbyButton = Player.PlayerGui:WaitForChild("ButtonGui").Lobby
  18. local ExtraLifeUI = Player.PlayerGui:WaitForChild("ExtraLifesFrame")
  19. local LevelsUI = Player.PlayerGui:WaitForChild("LevelsGui")
  20.  
  21.  
  22. local NextButton = SpectateFrame.BackRoundImage.NextButton
  23. local PlayerNameLabel = SpectateFrame.PlayerNameLabel
  24. local Camera = workspace.CurrentCamera
  25. local Reconnect
  26. local num --= 0
  27.  
  28. UI.Enabled = true
  29.  
  30. SpectateButton.MouseButton1Click:Connect(function()
  31.     ClickSound:Play()
  32.     if SpectateFrame.Visible then
  33.         SpectateButton.Position = UDim2.new(0.137, 0,0.43, 0)
  34.         DieButton.Position = UDim2.new(0.68,0,0.875,0)
  35.         ExtraLifeUI.Frame.Position = UDim2.new(0.53, 0,0.865, 0)
  36.         ShopButton.Visible = true
  37.         CodesButton.Visible = true
  38.         StatsButton.Visible = true
  39.         CoinsShopButton. Visible = true
  40.         GemsShopButton.Visible = true
  41.         --DieButton.Visible = true
  42.        
  43.         if Player.PlayerGui:WaitForChild("SpeedTimer") then
  44.             Player.PlayerGui:WaitForChild("SpeedTimer").Enabled = true
  45.         end
  46.        
  47.         SpectateButton.BackgroundColor3 = Color3.new(0.333333, 1, 0)
  48.         SpectateFrame.Visible = false
  49.         PlayerNameLabel.Text = Player.Name
  50.         Camera.CameraSubject = Player.Character.Humanoid
  51.         CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
  52.         num = 1
  53.         LevelsUI.Stage.Position = UDim2.new(0.155, 0,0.925, 0)
  54.         LevelsUI.StageDescription.Position = UDim2.new(0.04, 0,0.925, 0)
  55.  
  56.         LobbyButton.Position = UDim2.new(0.325,0,0.875,0)
  57.     else
  58.         SpectateButton.Position = UDim2.new(0.008, 0,0.43, 0)
  59.         ShopButton.Visible = false
  60.         CodesButton.Visible = false
  61.         StatsButton.Visible = false
  62.         CoinsShopButton. Visible = false
  63.         GemsShopButton.Visible = false
  64.         --DieButton.Visible = false
  65.         SpectateButton.BackgroundColor3 = Color3.new(1, 0, 0)
  66.         SpectateFrame.Visible = true
  67.         LevelsUI.Stage.Position = UDim2.new(0.155, 0,1.2, 0)
  68.         LevelsUI.StageDescription.Position = UDim2.new(0.04, 0,1.2, 0)
  69.         DieButton.Position = UDim2.new(0.68,0,1.275,0)
  70.         LobbyButton.Position = UDim2.new(0.325,0,1.275,0)
  71.         ExtraLifeUI.Frame.Position = UDim2.new(0.53, 0,1.2, 0)
  72.         --  PlayerNameLabel.Text = Player.Name
  73.         --  Camera.CameraSubject = Player.Character.Humanoid
  74.         num = 2
  75.         local Players = game.Players:GetPlayers()
  76.         local all = #Players   
  77.         if not Players[num] then   
  78.             PlayerNameLabel.Text = Player.Name
  79.             Camera.CameraSubject = Player.Character.Humanoid
  80.             CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
  81.         else
  82.             num = 2    
  83.             Camera.CameraSubject = Players[num].Character.Humanoid
  84.             PlayerNameLabel.Text = Players[num].Name
  85.             CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
  86.         end
  87.        
  88.         if Player.PlayerGui:WaitForChild("SpeedTimer") then
  89.             Player.PlayerGui:WaitForChild("SpeedTimer").Enabled = false
  90.         end
  91.     end
  92. end)
  93.  
  94. PreviousButton.MouseButton1Click:Connect(function()
  95.     ClickSound:Play()
  96.     local Players = game.Players:GetPlayers()
  97.     local all = #Players
  98.     num = num - 1
  99.     if num < 1 then
  100.         num = all
  101.     end
  102.     Camera.CameraSubject = Players[num].Character.Humanoid
  103.     PlayerNameLabel.Text = Players[num].Name
  104. end)
  105.  
  106. NextButton.MouseButton1Click:Connect(function()
  107.     ClickSound:Play()
  108.     local Players = game.Players:GetPlayers()
  109.     local all = #Players
  110.     num = num + 1
  111.     if num > all then
  112.         num = 1
  113.     end
  114.     Camera.CameraSubject = Players[num].Character.Humanoid
  115.     PlayerNameLabel.Text = Players[num].Name
  116. end)
  117.  
  118. Player.CharacterAdded:Connect(function(Char)
  119.     if SpectateFrame.Visible then
  120.         local Players = game.Players:GetPlayers()
  121.         if Char then
  122.             if Players[num] then
  123.                 if Players[num].Name == Char.Name then
  124.                 Camera.CameraSubject = Char:WaitForChild("Humanoid")
  125.                 PlayerNameLabel.Text = Char.Name
  126.                 elseif Players[num].Name ~= Char.Name then
  127.                     local OtherPlayerName = Players[num].Name
  128.                     if game.Workspace:FindFirstChild(OtherPlayerName) then
  129.                         task.wait(.1)
  130.                         local OtherCharacter = game.Workspace:FindFirstChild(OtherPlayerName)
  131.                         Camera.CameraSubject = OtherCharacter:WaitForChild("Humanoid")
  132.                         PlayerNameLabel.Text = OtherCharacter.Name
  133.                     end
  134.                 end
  135.             end
  136.         end
  137.     end
  138. end)
  139.  
  140. game.Players.ChildRemoved:Connect(function(LeftPlayer)
  141. --  warn(LeftPlayer.Name)
  142.     if SpectateFrame.Visible then
  143.         if PlayerNameLabel.Text == LeftPlayer.Name then
  144.             local Players = game.Players:GetPlayers()
  145.             if Players[num] then
  146.                 local OtherPlayerName = Players[num].Name
  147.                 if game.Workspace:FindFirstChild(OtherPlayerName) then
  148.                     task.wait(.1)  
  149.                     local OtherCharacter = game.Workspace:FindFirstChild(OtherPlayerName)
  150.                     Camera.CameraSubject = OtherCharacter:WaitForChild("Humanoid")
  151.                     PlayerNameLabel.Text = OtherCharacter.Name
  152.                 end
  153.             else
  154.                 num = 2
  155.                 PlayerNameLabel.Text = Player.Name
  156.                 Camera.CameraSubject = Player.Character.Humanoid
  157.             end
  158.         end
  159.     end
  160. end)
  161.  
  162. task.wait(10)
  163.  
  164. RunService.RenderStepped:Connect(function()
  165.     local Players = game.Players:GetPlayers()
  166.     if num ~= 0 then   
  167.         if Players[num] then       
  168.          if Players[num].Name ~= Player.Name then      
  169.                 local OtherPlayerName = Players[num].Name
  170.                 if game.Workspace:FindFirstChild(OtherPlayerName) then
  171.                     local OtherCharacter = game.Workspace:FindFirstChild(OtherPlayerName)
  172.                     Camera.CameraSubject = OtherCharacter:WaitForChild("Humanoid")
  173.                     PlayerNameLabel.Text = OtherCharacter.Name
  174.                end
  175.             end
  176.         end
  177.     end
  178.     task.wait(.1)  
  179. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement