Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- supremebloxboy
- Main game loop script for my freeze tag game.
- ]]
- task.wait(1)
- -- Main game variables and settings
- local MapList = game:GetService("ServerStorage").Maps:GetChildren()
- local GameTime = nil
- local GameStatus = "None"
- local CurrentMap = workspace.Map
- local PlayerEvents = {}
- local PlayWithAI = true
- local ShouldAssignTeams = false
- local RedAI = 20
- local BlueAI = 20
- -- function to spawn blue and red team players onto map
- -- Teams are either chosen by the player or assigned by the game, dependent on the variable ShouldAssignTeams
- local function SpawnPlayer(Player)
- -- if you have the Red Team tag you will spawn of a random red spawn
- if Player.Character.Humanoid:GetAttribute("Team") == "Red" then
- Player.Character.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.RedSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.RedSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
- -- if you have the Blue Team tag you will spawn of a random blue spawn
- elseif Player.Character.Humanoid:GetAttribute("Team") == "Blue" then
- Player.Character.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.BlueSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.BlueSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
- end
- -- if the player does not have the freeze gun one is provided
- if not Player.Backpack:FindFirstChild("FreezeGun") then
- game.ServerStorage.FreezeGun:Clone().Parent = Player.Backpack
- end
- end
- -- if assign teams is true, if not player chooses
- local function AssignTeams()
- -- loop through all players
- for i,v in pairs(game.Players:GetPlayers()) do
- -- if players are in the lobby and not in an active game
- if v.Team.Name == "Lobby" then
- -- if the number of red team members is greater than blue, make player blue
- if #game.Teams.Red:GetPlayers() > #game.Teams.Blue:GetPlayers() then
- v.Team = game.Teams.Blue
- -- if the number of blue team members is greater than red, make player red
- -- this is so teams arent imbalanced
- elseif #game.Teams.Red:GetPlayers() < #game.Teams.Blue:GetPlayers() then
- v.Team = game.Teams.Red
- else
- -- else if teams are even choose random team
- local ran = math.random(1,2)
- if ran == 1 then
- v.Team = game.Teams.Red
- else
- v.Team = game.Teams.Blue
- end
- end
- end
- end
- end
- -- check if all members of a team are frozen, if true game over
- local function CheckTags()
- local RedTagged = true
- local BlueTagged = true
- -- Checks if blue team is frozen
- -- loop through workspace and find all blue team players and check if tagged, if one blue player is not frozen, break.
- for i,v in pairs(workspace:GetChildren()) do
- if v:IsA("Model") and v:FindFirstChild("Humanoid") then
- if v.Humanoid:GetAttribute("Team") == "Blue" then
- if v.Humanoid:GetAttribute("Tagged") == false then
- BlueTagged = false
- break
- end
- end
- end
- end
- -- checks if red team is frozen
- -- same as before but with red team, if one red member is unfrozen then break
- for i,v in pairs(workspace:GetChildren()) do
- if v:IsA("Model") and v:FindFirstChild("Humanoid") then
- if v.Humanoid:GetAttribute("Team") == "Red" then
- if v.Humanoid:GetAttribute("Tagged") == false then
- RedTagged = false
- break
- end
- end
- end
- end
- -- return results, blue team or red team frozen or none
- if RedTagged then
- return true, "red"
- elseif BlueTagged then
- return true, "blue"
- else
- return false
- end
- end
- -- if PlayingWithAi is true, spawn ai
- local function SpawnAI()
- -- Configure and Spawn red ai
- -- for loop creating the number of ai needed
- for i = 1, RedAI do
- -- cloning ai from serverstorage
- local AI = game.ServerStorage.AI:Clone()
- -- changing body colors to match team color
- AI["Body Colors"].HeadColor3 = Color3.fromRGB(255,0,0)
- AI["Body Colors"].TorsoColor3 = Color3.fromRGB(255,0,0)
- AI["Body Colors"].LeftArmColor3 = Color3.fromRGB(255,0,0)
- AI["Body Colors"].LeftLegColor3 = Color3.fromRGB(255,0,0)
- AI["Body Colors"].RightArmColor3 = Color3.fromRGB(255,0,0)
- AI["Body Colors"].RightLegColor3 = Color3.fromRGB(255,0,0)
- -- setting attributes of team and tagged value
- AI.Humanoid:SetAttribute("Team", "Red")
- AI.Humanoid:SetAttribute("Tagged", false)
- -- setting map to the map the ai is spawned in
- AI.Map.Value = CurrentMap.Value
- -- setting pathfind value to a random enemy spawnlocation for ai pathfinding to the enemy base
- AI.PathFind.Value = CurrentMap.Value.Spawns.BlueSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.BlueSpawns:GetChildren())]
- -- finally spawn ai at random blue spawn location
- AI.Parent = workspace
- AI.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.RedSpawns:GetChildren()[Random.new():NextInteger(1, #CurrentMap.Value.Spawns.RedSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
- end
- -- Configure and spawn blue ai
- -- literally the exact same thing as before but with blue ai so i wont comment here
- for i = 1, BlueAI do
- local AI = game.ServerStorage.AI:Clone()
- AI["Body Colors"].HeadColor3 = Color3.fromRGB(0,0,255)
- AI["Body Colors"].TorsoColor3 = Color3.fromRGB(0,0,255)
- AI["Body Colors"].LeftArmColor3 = Color3.fromRGB(0,0,255)
- AI["Body Colors"].LeftLegColor3 = Color3.fromRGB(0,0,255)
- AI["Body Colors"].RightArmColor3 = Color3.fromRGB(0,0,255)
- AI["Body Colors"].RightLegColor3 = Color3.fromRGB(0,0,255)
- AI.Humanoid:SetAttribute("Team", "Blue")
- AI.Humanoid:SetAttribute("Tagged", false)
- AI.Map.Value = CurrentMap.Value
- AI.PathFind.Value = CurrentMap.Value.Spawns.RedSpawns:GetChildren()[math.random(#CurrentMap.Value.Spawns.RedSpawns:GetChildren())]
- AI.Parent = workspace
- AI.HumanoidRootPart.CFrame = CurrentMap.Value.Spawns.BlueSpawns:GetChildren()[Random.new():NextInteger(1, #CurrentMap.Value.Spawns.BlueSpawns:GetChildren())].CFrame + Vector3.new(0,3,0)
- end
- end
- -- current time used to track game time
- local EventSink = time()
- -- Main Game Loop
- while task.wait() do
- -- GameStatus None by default, Start game.
- if GameStatus == "None" then
- -- if not enough players and not playing with ai, game waits for more players before starting
- if #game.Players:GetPlayers() < 2 and not PlayWithAI then
- -- every 5 seconds game tells clients there is not enough players to start
- if (time() - EventSink) > 5 then
- game.ReplicatedStorage.GameStatus:FireAllClients("NEP")
- EventSink = time()
- end
- else -- elseif enough players to start or playing with ai, game start
- task.wait(5)
- -- choose random map to load
- local Map = MapList[math.random(#MapList)]:Clone()
- CurrentMap.Value = Map
- Map.Parent = workspace
- -- if ShouldAssignTeams then AssignTeams function is called and team will be auto assigned, else teams are manually chosen by players
- if ShouldAssignTeams then
- AssignTeams()
- end
- -- tell clients game is almost started
- game.ReplicatedStorage.GameStatus:FireAllClients("GameReady")
- task.wait(5)
- -- loop getting players and spawning them in the map
- for i,v in pairs(game.Players:GetPlayers()) do
- if v.Character and v.Character:FindFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 then
- -- Allow respawning, currently disabled
- --table.insert(PlayerEvents, v.CharacterAdded:Connect(function()
- -- SpawnPlayer(v)
- --end))
- -- Spawn once
- SpawnPlayer(v)
- end
- end
- -- spawn ai
- SpawnAI()
- -- game started
- GameTime = time()
- GameStatus = "Started"
- -- tell clients game is started
- game.ReplicatedStorage.GameStatus:FireAllClients("GameStart")
- end
- elseif GameStatus == "Started" then
- -- Game is started
- -- checks if all players on a team are frozen
- local bool, team = CheckTags()
- --if times up game over
- if time() - GameTime > 300 and not bool then
- -- Times up game over, inform clients
- game.ReplicatedStorage.GameStatus:FireAllClients("GameEnded", "Times up")
- GameStatus = "Ended"
- elseif bool then
- -- if red or blue is all frozen game over, other team wins
- if team == "red" then
- -- if red team is frozen, tell players blue team wins
- game.ReplicatedStorage.GameStatus:FireAllClients("GameEnded", "Blue Team Won")
- elseif team == "blue" then
- -- if blue team is frozen, tell players red team wins
- game.ReplicatedStorage.GameStatus:FireAllClients("GameEnded", "Red Team Won")
- end
- -- game over
- GameStatus = "Ended"
- end
- elseif GameStatus == "Ended" then
- -- game over cleanup
- -- Respawning currently disabled
- --for i,v in pairs(PlayerEvents) do
- -- if v then
- -- v:Disconnect()
- -- end
- --end
- task.wait(3)
- -- respawn all players and destroy ai
- -- loop through workspace to find players
- for i,v in pairs(workspace:GetChildren()) do
- if v:IsA("Model") and v:FindFirstChild("Humanoid") then
- -- if is player and has tag Team
- if v.Humanoid:GetAttribute("Team") then
- -- unfreeze player and set health to 0, respawning them
- game.ReplicatedStorage.ServerTag:Fire(v, false)
- v.Humanoid.Health = 0
- -- if ai then destroy
- if v.Name == "AI" then
- v:Destroy()
- end
- end
- end
- end
- -- destroy map, and reset GameStatus, New game starting soon
- CurrentMap.Value:Destroy()
- CurrentMap.Value = nil
- GameStatus = "None"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement