Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is this script alright and can I make it so that you die if nobody wins the match?

Asked by 8 years ago

local Maps = { -- Map names go in here; Maps go into lighting "Grassy Field" , "Siege" , "Studio" } local UsedMaps = {} local CurrentMap = nil local LobbyPosition = Vector3.new(0, 0, 0) -- Position of the LOBBY local Blue = BrickColor.new("Bright blue") -- Color of team Blue local Red = BrickColor.new("Bright red") -- Color of team Red

local IntermissionTime = 10 -- seconds local RoundTime = 120 -- seconds local MinimumNumPlayersToStart = 1 -- players

-- Code to equip players (with tools, etc) function EquipPlayers() GiveToolToTeam(game.Lighting.Dagger, Red) GiveToolToTeam(game.Lighting.Dagger, Blue) GiveToolToTeam(game.Lighting.Dagger, "ALL") end

-- Code to award winners! function AwardWinners(Winners) -- point awards go here

local winnerNames = GetWinnerNames(Winners)
local M = Instance.new("Message", workspace)
M.Text = "The winners are: " .. table.concat(winnerNames, ", ") .. "!"
wait(4)
M:Destroy()

end

-- Utility local Alive = { Red = {}, Blue = {} }

function CombineTables(A, B) local r = {} for i, v in ipairs(A) do table.insert(r, v) end for i, v in ipairs(B) do table.insert(r, v) end return r end

function RemoveFromTable(Table, Value) for _, v in ipairs(Table) do if v == Value then table.remove(Table, _) return true end end end

function GiveToolToTeam(Tool, Team) if Team == Blue then for _, Player in pairs(Alive.Blue) do Tool:Clone().Parent = Player.Backpack end elseif Team == Red then for _, Player in pairs(Alive.Red) do Tool:Clone().Parent = Player.Backpack end else for _, Player in pairs(Alive.Blue) do Tool:Clone().Parent = Player.Backpack end for _, Player in pairs(Alive.Red) do Tool:Clone().Parent = Player.Backpack end end end

function GetWinnerNames(winners) local r = {} for _, v in ipairs(winners) do table.insert(r, v.Name) end return r end

function KillAllAlive() for _, plr in pairs(Alive.Red) do plr.Character:BreakJoints() end for _, plr in pairs(Alive.Blue) do plr.Character:BreakJoints() end end

function Teleport(plr, pos) local v = Vector3.new(math.random(-10, 10), 0, math.random(-10, 10)) pcall(function() plr.Character:MoveTo(pos + v) end) end

function TeleportAll(Position) for _, plr in pairs(game.Players:GetPlayers()) do Teleport(plr, Position) end end

function RandomizeTeams() local players = game.Players:GetPlayers() for i = 1, math.random(10, 20) do local a, b = math.random(1, #players), math.random(1, #players) local temp = players[a] players[a] = players[b] players[b] = temp end for _, plr in ipairs(players) do if plr.Character and plr.Character:FindFirstChild("Torso") then if plr.Character.Humanoid.Health > 0 then plr.Neutral = false if _%2 == 0 then plr.TeamColor = Blue table.insert(Alive.Blue, plr) else plr.TeamColor = Red table.insert(Alive.Red, plr) end end end end end

function RoundHasEnded() -- returns list of winners if #Alive.Red == 0 then return Alive.Blue elseif #Alive.Blue == 0 then return Alive.Red end end

function DetermineWinners() -- returns list of winners if #Alive.Red > #Alive.Blue then return Alive.Red elseif #Alive.Red < #Alive.Blue then return Alive.Blue else return CombineTables(Alive.Red, Alive.B) end end

-- Core function Intermission(Time) local M = Instance.new("Message", workspace) for t = Time, 0, -1 do M.Text = "Next match in " .. t wait(1) end M:Destroy()

SetupRound()

end

function SetupRound() if game.Players.NumPlayers < MinimumNumPlayersToStart then local M = Instance.new("Message", workspace) M.Text = "More players are needed to start!" wait(3) M:Destroy() Intermission(IntermissionTime) return end if #Maps == 0 then Maps = UsedMaps UsedMaps = {} end local r = math.random(1, #Maps) local mapName = Maps[r] table.remove(Maps, r) table.insert(UsedMaps, mapName) local map = game.Lighting[mapName]:Clone() map.Parent = game.Workspace CurrentMap = map

local M = Instance.new("Message", workspace)
M.Text = "Match chosen! " .. map.Name
wait(4)
M:Destroy()

BeginRound(map)

end

function BeginRound(Map) RandomizeTeams() for _, plr in pairs(Alive.Red) do Teleport(plr, Map.RSpawn.Position) end for _, plr in pairs(Alive.Blue) do Teleport(plr, Map.BSpawn.Position) end EquipPlayers()

WaitForRound(RoundTime)

end

function WaitForRound(Time) -- returns list of winners if Time < 0 then return end local H = Instance.new("Hint", workspace) local winners = nil for t = Time, 0, -0.25 do winners = RoundHasEnded() if winners then break end H.Text = math.floor(t) .. " " wait(0.25) end H:Destroy() if winners then EndRound(winners) else EndRound(DetermineWinners()) end end

function EndRound(Winners) AwardWinners(Winners) KillAllAlive() CurrentMap:Destroy() CurrentMap = nil end

-- Handling player spawn / death / leave game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(c) wait(0.1) c:WaitForChild("Torso") Teleport(p, LobbyPosition) c:WaitForChild("Humanoid").Died:connect(function() if not p.Neutral then if p.TeamColor == Blue then RemoveFromTable(Alive.Blue, p) else RemoveFromTable(Alive.Red, p) end end p.Neutral = true end) end) end)

game.Players.PlayerRemoving:connect(function(p) if not p.Neutral then if p.TeamColor == Blue then RemoveFromTable(Alive.Blue, p) else RemoveFromTable(Alive.Red, p) end end end)

-- Game while true do Intermission(IntermissionTime) end

I also want to know how to make it kill you if nobody wins the match because I tested it with a friend and it didn't end the match

Answer this question