Advertisement
supremebloxboy

Script for my Freeze Tag Game for Hidden Devs

Aug 10th, 2022 (edited)
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.39 KB | None | 0 0
  1. --[[
  2. supremebloxboy
  3. Main game loop script for my freeze tag game.
  4. ]]
  5.  
  6. task.wait(1)
  7.  
  8. -- Main game variables and settings
  9. local MapList = game:GetService("ServerStorage").Maps:GetChildren()
  10. local GameTime = nil
  11. local GameStatus = "None"
  12. local CurrentMap = workspace.Map
  13. local PlayerEvents = {}
  14. local PlayWithAI = true
  15. local ShouldAssignTeams = false
  16. local RedAI = 20
  17. local BlueAI = 20
  18.  
  19. -- function to spawn blue and red team players onto map
  20. -- Teams are either chosen by the player or assigned by the game, dependent on the variable ShouldAssignTeams
  21. local function SpawnPlayer(Player)
  22.     -- if you have the Red Team tag you will spawn of a random red spawn
  23.     if Player.Character.Humanoid:GetAttribute("Team") == "Red" then
  24.         Player.Character.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.RedSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.RedSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
  25.     -- if you have the Blue Team tag you will spawn of a random blue spawn
  26.     elseif Player.Character.Humanoid:GetAttribute("Team") == "Blue" then
  27.         Player.Character.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.BlueSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.BlueSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
  28.     end
  29.     -- if the player does not have the freeze gun one is provided
  30.     if not Player.Backpack:FindFirstChild("FreezeGun") then
  31.         game.ServerStorage.FreezeGun:Clone().Parent = Player.Backpack
  32.     end
  33. end
  34.  
  35. -- if assign teams is true, if not player chooses
  36. local function AssignTeams()
  37.     -- loop through all players
  38.     for i,v in pairs(game.Players:GetPlayers()) do
  39.         -- if players are in the lobby and not in an active game
  40.         if v.Team.Name == "Lobby" then
  41.             -- if the number of red team members is greater than blue, make player blue
  42.             if #game.Teams.Red:GetPlayers() > #game.Teams.Blue:GetPlayers() then
  43.                 v.Team = game.Teams.Blue
  44.              -- if the number of blue team members is greater than red, make player red
  45.              -- this is so teams arent imbalanced
  46.             elseif #game.Teams.Red:GetPlayers() < #game.Teams.Blue:GetPlayers() then
  47.                 v.Team = game.Teams.Red
  48.             else
  49.                 -- else if teams are even choose random team
  50.                 local ran = math.random(1,2)
  51.                 if ran == 1 then
  52.                     v.Team = game.Teams.Red
  53.                 else
  54.                     v.Team = game.Teams.Blue
  55.                 end
  56.             end
  57.         end
  58.     end
  59. end
  60.  
  61. -- check if all members of a team are frozen, if true game over
  62. local function CheckTags()
  63.     local RedTagged = true
  64.     local BlueTagged = true
  65.     -- Checks if blue team is frozen
  66.     -- loop through workspace and find all blue team players and check if tagged, if one blue player is not frozen, break.
  67.     for i,v in pairs(workspace:GetChildren()) do
  68.         if v:IsA("Model") and v:FindFirstChild("Humanoid") then
  69.             if v.Humanoid:GetAttribute("Team") == "Blue" then
  70.                 if v.Humanoid:GetAttribute("Tagged") == false then
  71.                     BlueTagged = false
  72.                     break
  73.                 end
  74.             end
  75.         end    
  76.     end
  77.     -- checks if red team is frozen
  78.     -- same as before but with red team, if one red member is unfrozen then break
  79.     for i,v in pairs(workspace:GetChildren()) do
  80.         if v:IsA("Model") and v:FindFirstChild("Humanoid") then
  81.             if v.Humanoid:GetAttribute("Team") == "Red" then
  82.                 if v.Humanoid:GetAttribute("Tagged") == false then
  83.                     RedTagged = false
  84.                     break
  85.                 end
  86.             end    
  87.         end
  88.     end
  89.     -- return results, blue team or red team frozen or none
  90.     if RedTagged then
  91.         return true, "red"
  92.     elseif BlueTagged then
  93.         return true, "blue"
  94.     else
  95.         return false
  96.     end
  97. end
  98.  
  99. -- if PlayingWithAi is true, spawn ai
  100. local function SpawnAI()
  101.     -- Configure and Spawn red ai
  102.     -- for loop creating the number of ai needed
  103.     for i = 1, RedAI do
  104.         -- cloning ai from serverstorage
  105.         local AI = game.ServerStorage.AI:Clone()
  106.         -- changing body colors to match team color
  107.         AI["Body Colors"].HeadColor3 = Color3.fromRGB(255,0,0)
  108.         AI["Body Colors"].TorsoColor3 = Color3.fromRGB(255,0,0)
  109.         AI["Body Colors"].LeftArmColor3 = Color3.fromRGB(255,0,0)
  110.         AI["Body Colors"].LeftLegColor3 = Color3.fromRGB(255,0,0)
  111.         AI["Body Colors"].RightArmColor3 = Color3.fromRGB(255,0,0)
  112.         AI["Body Colors"].RightLegColor3 = Color3.fromRGB(255,0,0)
  113.         -- setting attributes of team and tagged value
  114.         AI.Humanoid:SetAttribute("Team", "Red")
  115.         AI.Humanoid:SetAttribute("Tagged", false)
  116.         -- setting map to the map the ai is spawned in
  117.         AI.Map.Value = CurrentMap.Value
  118.         -- setting pathfind value to a random enemy spawnlocation for ai pathfinding to the enemy base
  119.         AI.PathFind.Value = CurrentMap.Value.Spawns.BlueSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.BlueSpawns:GetChildren())]
  120.         -- finally spawn ai at random blue spawn location
  121.         AI.Parent = workspace
  122.         AI.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.RedSpawns:GetChildren()[Random.new():NextInteger(1, #CurrentMap.Value.Spawns.RedSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
  123.     end
  124.     -- Configure and spawn blue ai
  125.     -- literally the exact same thing as before but with blue ai so i wont comment here
  126.     for i = 1, BlueAI do
  127.         local AI = game.ServerStorage.AI:Clone()
  128.         AI["Body Colors"].HeadColor3 = Color3.fromRGB(0,0,255)
  129.         AI["Body Colors"].TorsoColor3 = Color3.fromRGB(0,0,255)
  130.         AI["Body Colors"].LeftArmColor3 = Color3.fromRGB(0,0,255)
  131.         AI["Body Colors"].LeftLegColor3 = Color3.fromRGB(0,0,255)
  132.         AI["Body Colors"].RightArmColor3 = Color3.fromRGB(0,0,255)
  133.         AI["Body Colors"].RightLegColor3 = Color3.fromRGB(0,0,255)
  134.         AI.Humanoid:SetAttribute("Team", "Blue")
  135.         AI.Humanoid:SetAttribute("Tagged", false)
  136.         AI.Map.Value = CurrentMap.Value
  137.         AI.PathFind.Value = CurrentMap.Value.Spawns.RedSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.RedSpawns:GetChildren())]
  138.         AI.Parent = workspace
  139.         AI.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.BlueSpawns:GetChildren()[Random.new():NextInteger(1, #CurrentMap.Value.Spawns.BlueSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
  140.     end
  141. end
  142.  
  143. -- current time used to track game time
  144. local EventSink = time()
  145. -- Main Game Loop
  146. while task.wait() do
  147.     -- GameStatus None by default, Start game.
  148.     if GameStatus == "None" then
  149.         -- if not enough players and not playing with ai, game waits for more players before starting
  150.         if #game.Players:GetPlayers() < 2 and not PlayWithAI then
  151.             -- every 5 seconds game tells clients there is not enough players to start
  152.             if (time() - EventSink) > 5 then
  153.                 game.ReplicatedStorage.GameStatus:FireAllClients("NEP")
  154.                 EventSink = time()
  155.             end
  156.         else -- elseif enough players to start or playing with ai, game start
  157.             task.wait(5)
  158.             -- choose random map to load
  159.             local Map = MapList[math.random(#MapList)]:Clone()
  160.             CurrentMap.Value = Map
  161.             Map.Parent = workspace
  162.             -- if ShouldAssignTeams then AssignTeams function is called and team will be auto assigned, else teams are manually chosen by players
  163.             if ShouldAssignTeams then
  164.                 AssignTeams()
  165.             end
  166.             -- tell clients game is almost started
  167.             game.ReplicatedStorage.GameStatus:FireAllClients("GameReady")
  168.  
  169.             task.wait(5)
  170.             -- loop getting players and spawning them in the map
  171.             for i,v in pairs(game.Players:GetPlayers()) do
  172.                 if v.Character and v.Character:FindFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 then
  173.                     -- Allow respawning, currently disabled
  174.                    
  175.                     --table.insert(PlayerEvents, v.CharacterAdded:Connect(function()
  176.                     --  SpawnPlayer(v)
  177.                     --end))
  178.                    
  179.                     -- Spawn once
  180.                     SpawnPlayer(v)
  181.                 end
  182.             end
  183.             -- spawn ai
  184.             SpawnAI()
  185.             -- game started
  186.             GameTime = time()
  187.             GameStatus = "Started"
  188.             -- tell clients game is started
  189.             game.ReplicatedStorage.GameStatus:FireAllClients("GameStart")
  190.         end
  191.     elseif GameStatus == "Started" then
  192.         -- Game is started
  193.         -- checks if all players on a team are frozen
  194.         local bool, team = CheckTags()
  195.         --if times up game over
  196.         if time() - GameTime > 300 and not bool then
  197.             -- Times up game over, inform clients
  198.             game.ReplicatedStorage.GameStatus:FireAllClients("GameEnded", "Times up")
  199.             GameStatus = "Ended"
  200.         elseif bool then
  201.             -- if red or blue is all frozen game over, other team wins
  202.             if team == "red" then
  203.                 -- if red team is frozen, tell players blue team wins
  204.                 game.ReplicatedStorage.GameStatus:FireAllClients("GameEnded", "Blue Team Won")
  205.             elseif team == "blue" then
  206.                 -- if blue team is frozen, tell players red team wins
  207.                 game.ReplicatedStorage.GameStatus:FireAllClients("GameEnded", "Red Team Won")
  208.             end
  209.             -- game over
  210.             GameStatus = "Ended"
  211.         end
  212.  
  213.     elseif GameStatus == "Ended" then
  214.         -- game over cleanup
  215.         -- Respawning currently disabled
  216.         --for i,v in pairs(PlayerEvents) do
  217.         --  if v then
  218.         --      v:Disconnect()
  219.         --  end
  220.         --end
  221.        
  222.         task.wait(3)
  223.         -- respawn all players and destroy ai
  224.         -- loop through workspace to find players
  225.         for i,v in pairs(workspace:GetChildren()) do
  226.             if v:IsA("Model") and v:FindFirstChild("Humanoid") then
  227.                 -- if is player and has tag Team
  228.                 if v.Humanoid:GetAttribute("Team") then
  229.                     -- unfreeze player and set health to 0, respawning them
  230.                     game.ReplicatedStorage.ServerTag:Fire(v, false)
  231.                     v.Humanoid.Health = 0
  232.                     -- if ai then destroy
  233.                     if v.Name == "AI" then
  234.                         v:Destroy()
  235.                     end
  236.                 end
  237.             end
  238.         end
  239.         -- destroy map, and reset GameStatus, New game starting soon
  240.         CurrentMap.Value:Destroy()
  241.         CurrentMap.Value = nil
  242.         GameStatus = "None"
  243.     end
  244. end
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement