chima95

Freeze Tag - Game Controller

Apr 17th, 2021
733
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local CurrentMap = workspace:WaitForChild("CurrentMap")
  2.  
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local Maps = ReplicatedStorage:WaitForChild("Maps")
  5. --Events
  6. local SendMessage = ReplicatedStorage:WaitForChild("SendMessage")
  7.  
  8. local GameController = {
  9.     GamePlaying = false,
  10.     TaggersHaveTeleported = false,
  11.     Taggers = {},
  12.     Runners = {},
  13. }
  14.  
  15. function GameController:PickMap()
  16.     local Map = Maps:GetChildren()[math.random(1, #Maps:GetChildren())]
  17.     return Map
  18. end
  19.  
  20. function GameController:AddMap()
  21.     CurrentMap:ClearAllChildren()
  22.    
  23.     local Map = GameController.PickMap():Clone()
  24.     Map.Parent = CurrentMap
  25.    
  26.     local IceBlocks = Instance.new("Folder", CurrentMap)
  27.     IceBlocks.Name = "IceBlocks"
  28.    
  29.     return Map
  30. end
  31.  
  32. function GameController:PickTeams()
  33.     local Taggers = {}
  34.     local Runners = {}
  35.    
  36.     local Players = game.Players:GetPlayers()
  37.     local PlayerCount = #Players
  38.    
  39.     if PlayerCount == 1 then
  40.     elseif PlayerCount >= 2 then
  41.         local TaggerCount = math.floor(PlayerCount/2)
  42.        
  43.         for Index, Player in pairs(Players) do
  44.             if Index <= TaggerCount then
  45.                 --Tagger
  46.                 table.insert(Taggers, Player)
  47.                
  48.             elseif Index > TaggerCount then
  49.                 --Runner
  50.                 table.insert(Runners, Player)
  51.             end
  52.         end
  53.     end
  54.    
  55.     return Taggers, Runners
  56. end
  57.  
  58. function GameController:TeleportRunners(Map, Runners)
  59.     local TaggerSpawn = Map:FindFirstChild("TaggerSpawn")
  60.     local RunnerSpawns = Map:FindFirstChild("RunnerSpawns")
  61.    
  62.     if TaggerSpawn and RunnerSpawns then
  63.         for _, Runner in pairs(Runners) do
  64.             local Character = Runner.Character or Runner.CharacterAdded:wait()
  65.             if Character then
  66.                 local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
  67.                 local RandomSpawn = RunnerSpawns:GetChildren()[math.random(1, #RunnerSpawns:GetChildren())]
  68.                 HumanoidRootPart.CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
  69.             end
  70.         end
  71.     else
  72.         warn("No Tagger Spawn And/Or No Runner Spawns")
  73.     end
  74. end
  75.  
  76. function GameController:TaggerReseted(TaggerToMove, TaggerSpawn)
  77.     if GameController.GamePlaying == true and GameController.TaggersHaveTeleported then
  78.         for _, Tagger in pairs(GameController.Taggers) do
  79.             if Tagger == TaggerToMove then
  80.                 local Character = TaggerToMove.Character or TaggerToMove.CharacterAdded:wait()
  81.                 if Character and TaggerSpawn ~= nil then
  82.                     repeat
  83.                         wait()
  84.                         Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(TaggerSpawn.Position)
  85.                     until Character:WaitForChild("HumanoidRootPart").CFrame == CFrame.new(TaggerSpawn.Position)
  86.                 end
  87.             end
  88.         end
  89.     end
  90. end
  91.  
  92. function GameController:TeleportTaggers(Map, Taggers)
  93.     GameController.TaggersHaveTeleported = true
  94.    
  95.     local TaggerSpawn = Map:FindFirstChild("TaggerSpawn")
  96.     local RunnerSpawns = Map:FindFirstChild("RunnerSpawns")
  97.  
  98.     if TaggerSpawn and RunnerSpawns then
  99.         for _, Tagger in pairs(Taggers) do
  100.             local Character = Tagger.Character or Tagger.CharacterAdded:wait()
  101.             if Character then
  102.                 local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
  103.                 HumanoidRootPart.CFrame = CFrame.new(TaggerSpawn.Position + Vector3.new(0, 3, 0))
  104.             end
  105.            
  106.             Tagger.CharacterAdded:connect(function()
  107.                 GameController:TaggerReseted(Tagger, TaggerSpawn)
  108.             end)
  109.         end
  110.     else
  111.         warn("No Tagger Spawn And/Or No Runner Spawns")
  112.     end
  113. end
  114.  
  115. function GameController:SendRunnersMessage(MessageData)
  116.     for _, Runner in pairs(GameController.Runners) do
  117.         SendMessage:FireClient(Runner, MessageData)
  118.     end
  119. end
  120.  
  121.  
  122. function GameController:SendTaggersMessage(MessageData)
  123.     for _, Tagger in pairs(GameController.Taggers) do
  124.         SendMessage:FireClient(Tagger, MessageData)
  125.     end
  126. end
  127.  
  128. function GameController:PlayerFrozen()
  129.     local IceBlocks = workspace.CurrentMap:WaitForChild("IceBlocks")
  130.     if IceBlocks then
  131.         if #GameController.Runners == #IceBlocks:GetChildren() then
  132.             GameController.EndGame()
  133.         end
  134.     end
  135. end
  136.  
  137. function GameController:GiveTaggerAbility(Character)
  138.     local Player = game.Players:GetPlayerFromCharacter(Character)
  139.    
  140.     for _, Tagger in pairs(GameController.Taggers) do
  141.    
  142.         if Tagger == Player then
  143.             local TaggerAbilityScript = script:WaitForChild("TaggerAbility"):Clone()
  144.             TaggerAbilityScript.Parent = Character:WaitForChild("HumanoidRootPart")
  145.             TaggerAbilityScript.Disabled = false
  146.            
  147.             return TaggerAbilityScript
  148.         end
  149.        
  150.     end
  151. end
  152.  
  153. function GameController:GiveTaggerAbilities(Taggers)
  154.     for _, Tagger in pairs(Taggers) do
  155.         local Character = Tagger.Character or Tagger.CharacterAdded:wait()
  156.         if Character then
  157.             local TaggerAbilityScript = GameController:GiveTaggerAbility(Character)
  158.         end
  159.        
  160.         Tagger.CharacterAdded:connect(GameController.GiveTaggerAbility)
  161.     end
  162. end
  163.  
  164. function GameController:EndGame()
  165.     GameController.GamePlaying = false
  166.     GameController.TaggersHaveTeleported = false
  167. end
  168.  
  169. function GameController:PlayerJoining(Player)
  170.     if GameController.GamePlaying == false and #game.Players:GetChildren() == 1 then
  171.         SendMessage:FireClient(Player, {Message="2 players are required"})
  172.     end
  173. end
  174.  
  175. function GameController:PlayerLeaving(Player)
  176.     for Index, Tagger in pairs(GameController.Taggers) do
  177.         if Tagger == Player then
  178.             table.remove(GameController.Taggers, Index)
  179.         end
  180.     end
  181.    
  182.     for Index, Runner in pairs(GameController.Runners) do
  183.         if Runner == Player then
  184.             table.remove(GameController.Runners, Index)
  185.         end
  186.     end
  187.    
  188.     if GameController.GamePlaying == true then
  189.         if #GameController.Taggers + #GameController.Runners <= 1 then
  190.             GameController:EndGame()
  191.         end
  192.        
  193.         if #GameController.Taggers == 0 then
  194.             GameController:EndGame()
  195.         end
  196.        
  197.         if #GameController.Runners == 0 then
  198.             GameController:EndGame()
  199.         end
  200.     end
  201. end
  202.  
  203. function GameController:Initiate()
  204.     while wait() do
  205.         GameController.GamePlaying = false
  206.         if #game.Players:GetPlayers() >= 2 then
  207.             SendMessage:FireAllClients({Message="Game is starting in 5 seconds"})
  208.             GameController.GamePlaying = true
  209.            
  210.             local Map = GameController:AddMap()
  211.            
  212.             wait(5) -- Time for the map to load in
  213.            
  214.             local Taggers, Runners = GameController:PickTeams()
  215.            
  216.             GameController.Taggers = Taggers
  217.             GameController.Runners = Runners
  218.  
  219.             GameController:TeleportRunners(Map, Runners)
  220.            
  221.             GameController:SendTaggersMessage({Message="Prepare to freeze players!"})
  222.             GameController:SendRunnersMessage({Message="Find a spot to hide before the taggers appear!"})
  223.            
  224.             wait(15) --Amount of time Runners have to find a hiding spot
  225.            
  226.             GameController:SendTaggersMessage({Message="Touch a player to freeze them!"})
  227.             GameController:SendRunnersMessage({Message="Try not to get freezed by the taggers!"})
  228.            
  229.             GameController:TeleportTaggers(Map, Taggers)
  230.             GameController:GiveTaggerAbilities(Taggers)
  231.            
  232.             local StartingTime = tick()
  233.  
  234.             while wait() do
  235.                 if tick() - StartingTime >= 60 then
  236.                     GameController.EndGame()
  237.                 end
  238.                 if GameController.GamePlaying == false then
  239.                     break
  240.                 end
  241.             end
  242.            
  243.             --Move all players to lobby and reset humanoid status
  244.            
  245.             for _, Tagger in pairs(GameController.Taggers) do
  246.                 if Tagger.Character then
  247.                     local HumanoidRootPart = Tagger.Character:FindFirstChild("HumanoidRootPart")
  248.                     HumanoidRootPart.Anchored = false
  249.                     if HumanoidRootPart then
  250.                         local TaggerAbility = HumanoidRootPart:FindFirstChild("TaggerAbility")
  251.                         if TaggerAbility then
  252.                             TaggerAbility:Destroy()
  253.                         end
  254.                         HumanoidRootPart.CFrame = CFrame.new(workspace.SpawnLocation.Position)
  255.                     end
  256.                 end
  257.             end
  258.            
  259.             for _, Runner in pairs(GameController.Runners) do
  260.                 if Runner.Character then
  261.                     local HumanoidRootPart = Runner.Character:FindFirstChild("HumanoidRootPart")
  262.                     HumanoidRootPart.Anchored = false
  263.                     if HumanoidRootPart then
  264.                         HumanoidRootPart.Anchored = false
  265.                         HumanoidRootPart.CFrame = CFrame.new(workspace.SpawnLocation.Position)
  266.                     end
  267.                 end
  268.             end
  269.            
  270.             GameController.Taggers = {}
  271.             GameController.Runners = {}
  272.            
  273.             SendMessage:FireAllClients({Message="The game has ended!"})
  274.            
  275.             wait(10) -- Game intermission
  276.         end
  277.     end
  278. end
  279.  
  280. return GameController
  281.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×