Advertisement
Sungmingamerpro13

Bus MainScript

Jan 3rd, 2023
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 3.97 KB | None | 0 0
  1. local TS = game:GetService("TeleportService")
  2. local TweenService = game:GetService("TweenService")
  3. local placeId = (10760289632)
  4. local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent--Instert a remote event in ReplicatedStorage and name it to LeaveGuiEvent
  5. local TransitionEvent = game.ReplicatedStorage.TransitionEvent--Instert a remote event in ReplicatedStorage and name it to TransitionEvent
  6. local list = {}
  7. local gui = script.Parent.GuiPart.SurfaceGui
  8. local billboard = script.Parent.billboardPart.billboardGui
  9. local timer
  10. local teleporting = false
  11. local spawnTeleport = script.Parent.spawn
  12. local Seats = script.Parent.Parent.Bus.Seats:GetChildren()
  13. local Min_Players = 1
  14. local Max_Players = 12
  15. local PlayersCount = script.Parent.PlayersCount
  16. local Debounce = false
  17.  
  18. local function updateGui()
  19.     gui.Frame.players.Text = #list
  20.     billboard.Frame.players.Text = #list
  21.     PlayersCount.Value = #list
  22. end
  23.  
  24. local function removeFromList(character)
  25.     for i=1,#list do
  26.         if list[i] == character.Name then
  27.             table.remove(list,i)
  28.             updateGui()
  29.         end
  30.     end
  31. end
  32.  
  33. local function teleportPlayers()
  34.     if #list >= Min_Players then
  35.         script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "Teleporting..."
  36.         script.Parent.billboardPart.billboardGui.Frame.Status.Text = "Teleporting..."
  37.         script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(1,0,0)
  38.         local playersToTeleport = {}
  39.         local teleportTime = 0
  40.         for i=1,#list do
  41.             if game.Players:findFirstChild(list[i]) then
  42.                 table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
  43.                 TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
  44.             else
  45.                 table.remove(list,i)   
  46.             end
  47.         end
  48.         local code = TS:ReserveServer(placeId)
  49.         teleporting = true
  50.         TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
  51.         repeat wait() until #list <= 0
  52.         script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "READY"
  53.         script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(0.333333, 1, 0)
  54.         teleporting = false
  55.     else
  56.         billboard.Frame.players.Text = "1 Players Neeeded"
  57.         gui.Frame.players.Text = "1 Players Neeeded"
  58.         wait(2.5)
  59.         billboard.Frame.players.Text = #list.."/"..Max_Players
  60.         gui.Frame.players.Text = #list.."/"..Max_Players
  61.     end
  62. end
  63.  
  64. script.Parent.BusEnter.Touched:Connect(function(hit)
  65.     if hit.Parent:findFirstChild("Humanoid") then
  66.         if teleporting == false then
  67.             local char = hit.Parent
  68.             local player = game.Players:FindFirstChild(char.Name)
  69.             local alreadyExists = false
  70.            
  71.             for i=1,#list do
  72.                 if list[i] == char.Name then
  73.                     alreadyExists = true
  74.                 end
  75.             end
  76.            
  77.             if alreadyExists == false then
  78.                 if #list < Max_Players then -- Many Players have been teleported.
  79.                     table.insert(list,char.Name)
  80.                     char.PrimaryPart.CFrame = spawnTeleport.CFrame
  81.                     if Debounce then return end
  82.                     local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  83.                     for i, v in pairs(Seats) do
  84.                         if Seats.Occupant == nil then
  85.                             local Check = Instance.new("Weld")
  86.                             Check.Name = "Check"
  87.                             Check.Parent = player.Character
  88.                             Check.Parent.Humanoid.JumpPower = 0
  89.                             Check.Parent.Humanoid.WalkSpeed = 0
  90.                         end
  91.                     end
  92.                     Seats[math.random(1, #Seats)]:Sit(player.Character.Humanoid)
  93.                     updateGui()
  94.                     game.ServerStorage.ExitGui:Clone().Parent = player.PlayerGui
  95.                 end
  96.                 player.CharacterRemoving:connect(function(character)
  97.                     removeFromList(character)
  98.                 end)
  99.             end
  100.            
  101.         end
  102.     end
  103. end)
  104.  
  105. leaveGuiEvent.OnServerEvent:Connect(function(player)
  106.     player.Character.Humanoid.Sit = false
  107.     wait()
  108.     player:LoadCharacter()
  109. end)
  110.  
  111. PlayersCount:GetPropertyChangedSignal("Value"):Connect(function()
  112.     gui.Frame.players.Text = PlayersCount.Value.."/"..Max_Players
  113.     billboard.Frame.players.Text = PlayersCount.Value.."/"..Max_Players
  114. end)
  115.  
  116. while wait() do
  117.     timer = 41
  118.     for i=1,timer do
  119.         timer = timer - 1
  120.         gui.Frame.time.Text = timer
  121.         billboard.Frame.time.Text = timer
  122.         wait(1)
  123.     end
  124.     teleportPlayers()
  125. end
  126.  
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement