0% found this document useful (0 votes)
23 views8 pages

Team Management and Game Logic Script

The document contains a Lua script for managing a multiplayer game, including team assignment, balancing, player spawning, and game announcements. It defines constants for gameplay parameters, helper functions for team management, and a main game loop that periodically checks spawn conditions. The script also handles player resets and map regeneration when necessary, ensuring a smooth gaming experience.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Team Management and Game Logic Script

The document contains a Lua script for managing a multiplayer game, including team assignment, balancing, player spawning, and game announcements. It defines constants for gameplay parameters, helper functions for team management, and a main game loop that periodically checks spawn conditions. The script also handles player resets and map regeneration when necessary, ensuring a smooth gaming experience.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

-- Constants

local PlayersRequiredToStartAwardingPoints = 4
local SpawnDistanceThreshold = 500
local PointsAward = 100
local CooldownTime = 1 -- Cooldown in seconds

-- Helper Functions
local function getTeams()
local teams = {}
for _, team in pairs([Link]:GetTeams()) do
if [Link] then
[Link](teams, team)
end
end
return teams
end

local function assignPlayersToTeams()


local teams = getTeams()
local players = [Link]:GetPlayers()
local numberOfTeams = #teams
local numberOfPlayers = #players

if numberOfTeams == 0 or numberOfPlayers == 0 then


return
end

local playersPerTeam = [Link](numberOfPlayers / numberOfTeams)


local leftoverPlayers = numberOfPlayers - (playersPerTeam *
numberOfTeams)
local teamCounts = {}

for _, team in ipairs(teams) do


teamCounts[team] = 0
for i = 1, playersPerTeam do
local playerIndex = [Link](1, #players)
local player = players[playerIndex]
if player then
[Link] = [Link]
teamCounts[team] = teamCounts[team] + 1
[Link](players, playerIndex)
end
end
end

for i = 1, leftoverPlayers do
local playerIndex = [Link](1, #players)
local player = players[playerIndex]
local teamIndex = [Link](1, #teams)
local team = teams[teamIndex]
if player and team then
[Link] = [Link]
[Link](players, playerIndex)
[Link](teams, teamIndex)
end
end
end

local function balanceTeams()


local players = [Link]:GetPlayers()
local teams = getTeams()
local teamCounts = {}

if #teams == 0 or #players == 0 then


return
end

for _, team in pairs(teams) do


teamCounts[team] = 0
for _, player in pairs(players) do
if [Link] == [Link] then
teamCounts[team] = teamCounts[team] + 1
end
end
end

while true do
local smallestTeam = next(teamCounts)
local largestTeam = smallestTeam

for team, count in pairs(teamCounts) do


if count < teamCounts[smallestTeam] then
smallestTeam = team
end
if count > teamCounts[largestTeam] then
largestTeam = team
end
end

if (teamCounts[largestTeam] - teamCounts[smallestTeam]) > 1 then


for _, player in pairs(players) do
if [Link] == [Link] then
[Link] = [Link]
teamCounts[smallestTeam] =
teamCounts[smallestTeam] + 1
teamCounts[largestTeam] =
teamCounts[largestTeam] - 1
break
end
end
else
return
end
end
end

local function regenerateMap()


-- Assuming this function is called to regenerate the map
-- Make sure leaderstats are preserved and not reset
[Link] = true
end

local function resetAllPlayers()


for _, player in pairs([Link]:GetPlayers()) do
local character = [Link]
if character then
-- Reset player character
character:BreakJoints() -- Removes all joints in the
character, effectively resetting it
player:LoadCharacter() -- Reloads the player's character
end
end
end

local function manageSpawns()


local baseplate = workspace:FindFirstChild("Baseplate")
if baseplate then
local spawns = [Link]:GetDescendants()
local spawnsCount = 0

for _, spawn in ipairs(spawns) do


if spawn:IsA("SpawnLocation") then
if ([Link] - [Link]).magnitude >
SpawnDistanceThreshold then
spawn:Destroy()
else
spawnsCount = spawnsCount + 1
end
end
end

if spawnsCount <= 0 then


warn("Game might have broken, regenerating")
regenerateMap()
resetAllPlayers()
wait(CooldownTime) -- Wait for cooldown before allowing
further actions
end
else
for _, player in ipairs([Link]:GetPlayers()) do
player:Kick("Possible exploit, shutting down server")
end
end
end

local function teleportPlayersToTeams()


for _, player in pairs([Link]:GetPlayers()) do
local playerTeam =
[Link]:FindFirstChild([Link])
if playerTeam then
local spawnLocations =
[Link]:FindFirstChild([Link]):GetChildren()
local spawnLocation = nil

for _, location in pairs(spawnLocations) do


if location:IsA("SpawnLocation") then
spawnLocation = location
break
end
end

if spawnLocation then

[Link]:SetPrimaryPartCFrame([Link])
end
end
end
end

function AssignableTeams()
local assignableTeams = {}
for _, team in pairs([Link]:GetTeams()) do
if [Link] then
[Link](assignableTeams, team)
end
end
return assignableTeams
end

script:WaitForChild("ShuffleTeams").OnInvoke = function()
local teams = AssignableTeams()
local numberOfTeams = #teams
local playersList = [Link]:GetPlayers()
local numberOfPlayers = #playersList

-- Quit early if there are no teams, this avoids a divide by zero


if #teams == 0 or #playersList == 0 then
return
end

local playersPerTeam = [Link](numberOfPlayers / numberOfTeams)


local leftOverPlayers = numberOfPlayers - (playersPerTeam *
numberOfTeams)
local teamCount = {}

for _, team in ipairs(teams) do


teamCount[team] = 0
for i = 1, playersPerTeam do
local k = [Link](1, #playersList)
local player = playersList[k]
if player then
[Link] = [Link]
teamCount[team] = teamCount[team] + 1
[Link](playersList, k)
end
end
end

-- Now place the remaining players


for i = 1, leftOverPlayers do
local k, player = next(playersList)
local teamIndex = [Link](1, #teams)
local team = teams[teamIndex]
if player and teamIndex then
[Link] = [Link]
[Link](playersList, k)
[Link](teams, teamIndex)
end
end
end

-- Main Script Execution


local function createAnnouncement(text, teamColor, playVictorySound)
local color = [Link]
local soundService = game:GetService("SoundService")
local sound
if playVictorySound and not soundService:FindFirstChild("VictorySound") then
sound = [Link]("Sound")
[Link] = soundService
[Link] = "VictorySound"
[Link] = "rbxassetid://6286482559"
[Link] = 1
end

for _, player in ipairs([Link]:GetPlayers()) do


if player:FindFirstChild("PlayerGui") then
local gui = [Link]("ScreenGui")
[Link] = [Link]
[Link] = "AnnouncementGui"

local label = [Link]("TextLabel")


[Link] = gui
[Link] = [Link](1, 0, 0.1, 0)
[Link] = [Link](0, 0, 0.4, 0)
[Link] = 0.666
label.BackgroundColor3 = [Link](1, 1, 1) -- White color (RGB
values from 0 to 1)
label.TextColor3 = color
[Link] = [Link]
[Link] = 32
[Link] = text
[Link] = 0

if sound then
sound:Play()
end

wait(2) -- Show the message for 4 seconds

game:GetService("Debris"):AddItem(gui, 1)
end
end

if sound then
game:GetService("Debris"):AddItem(sound, 3)
end
end

local function setupGame()


local y = workspace:GetDescendants()

for s = 1, #y do
if y[s].Name == "Doomspires" then
local GVF = [Link]("Folder")
[Link] = y[s]
[Link] = "GameValues"
local TF = [Link]("Folder")
[Link] = GVF
[Link] = "Teams"
local TeamsLeftInt = [Link]("IntValue")
[Link] = GVF
[Link] = "TeamsLeft"

local k = y[s]:GetChildren()
for o = 1, #k do
if not k[o]:IsA("Script") and k[o].Name ~= "GameValues" and
k[o].Name ~= "Holder" then
local BCV = [Link]("BrickColorValue")
[Link] = TF
[Link] = k[o].Name
local SpawnVal = [Link]("IntValue")
[Link] = BCV
[Link] = "Spawns"
local u = k[o]:GetDescendants()
for h = 1, #u do
if u[h]:IsA("SpawnLocation") then
[Link] = u[h].TeamColor
[Link] =
#u[h].Parent:GetChildren()
end
end
end
end

[Link] = #TF:GetChildren()
local x = y[s]:GetDescendants()

for i = 1, #x do
if x[i]:IsA("SpawnLocation") then
x[i].AncestryChanged:Connect(function(SpawnPart)
local c = y[s]:WaitForChild("GameValues",
1):WaitForChild("Teams", 1):GetChildren()
for g = 1, #c do
if SpawnPart:IsA("SpawnLocation") and
c[g].Value == [Link] then
local spawns =
c[g]:WaitForChild("Spawns", 1)
[Link] = [Link] - 1

if [Link] == 0 then
local teamsLeft =
y[s]:WaitForChild("GameValues", 1):WaitForChild("TeamsLeft", 1)
[Link] =
[Link] - 1

[Link]:FindFirstChild(c[g].Name).AutoAssignable = false

createAnnouncement(c[g].Name .. " TEAM has been eliminated!", c[g].Value)

-- ✅ Team remaining
announcement
createAnnouncement("Teams
Remaining: " .. tostring([Link]), [Link]())
end
end
end

local teamsLeft =
y[s]:WaitForChild("GameValues", 1):WaitForChild("TeamsLeft", 1)
if [Link] == 1 then
local e = y[s]:WaitForChild("GameValues",
1):WaitForChild("Teams", 1):GetDescendants()
for j = 1, #e do
if e[j].Name == "Spawns" and
e[j].Value ~= 0 then
wait(2)
local winningTeamName =
e[j].[Link]
local winningTeamColor =
e[j].[Link]

createAnnouncement(winningTeamName .. " TEAM has won the game🏆",


winningTeamColor, true)

for _, player in
ipairs([Link]:GetPlayers()) do
if [Link] ==
winningTeamColor then
local playerStats
= player:FindFirstChild("leaderstats")
if playerStats
then
local wins =
playerStats:FindFirstChild("Wins")
if wins then

[Link] = [Link] + 1
end
end
end
end

regenerateMap()
resetAllPlayers()

-- ✅ Countdown with dot-dot-


dot movement
spawn(function()
for i = CooldownTime, 1,
-1 do
local dots =
[Link](".", (CooldownTime - i) % 4)

createAnnouncement("Restarting in " .. i .. " seconds" .. dots,


[Link]())
wait(2)
end
end)

wait(CooldownTime)
teleportPlayersToTeams()
return
end
end
end
end)
end
end
end
end
end
setupGame()

-- Main Game Loop


while wait(10) do
manageSpawns()
end

-- Team Shuffle and Balance Functions


script:WaitForChild("ShuffleTeams").OnInvoke = assignPlayersToTeams
script:WaitForChild("BalanceTeams").OnInvoke = balanceTeams

You might also like