I require some help, when Player 1 Destroys brick it accounts to hit brick destroyed total, when another player destroys bricks it sets Players 1’s to 0 and Updates Player 2 Brick’s destroyed, and it ends up in a endless cycle, It should be each Player is independent.
local TeamManager = {}
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
TeamManager.RedCount = Workspace:FindFirstChild("RedScore")
TeamManager.BlueCount = Workspace:FindFirstChild("BlueScore")
local serverModules = game.ServerStorage:WaitForChild("ModuleScripts")
local PlayerStats = require(serverModules:WaitForChild("PlayerStats"))
TeamManager.TeamContributions = {}
function TeamManager:InitializeTeams()
for _, player in pairs(Players:GetPlayers()) do
self:SetupPlayerTeam(player)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
self:SetupPlayerTeam(player)
end)
end)
end
function TeamManager:SetupPlayerTeam(player)
local character = player.Character or player.CharacterAdded:Wait()
local hasRed = character:GetAttribute("Red") == true
local hasBlue = character:GetAttribute("Blue") == true
if hasRed then
self.TeamContributions[player.Name] = { team = "Red", bricks = 0, exp = 0, coins = 0 }
elseif hasBlue then
self.TeamContributions[player.Name] = { team = "Blue", bricks = 0, exp = 0, coins = 0 }
end
end
function TeamManager:AddContribution(player, contributionAmount)
local contributionData = self.TeamContributions[player.Name]
if contributionData then
local playerTeam = contributionData.team
contributionData.bricks = contributionData.bricks + contributionAmount
if playerTeam == "Red" then
self.RedCount.Value = self.RedCount.Value + contributionAmount
elseif playerTeam == "Blue" then
self.BlueCount.Value = self.BlueCount.Value + contributionAmount
end
end
end
function TeamManager:DetermineWinnerByBlownUpParts()
local redBlownUpCount = self.RedCount.Value
local blueBlownUpCount = self.BlueCount.Value
if redBlownUpCount > blueBlownUpCount then
self:RewardWin("Red")
elseif blueBlownUpCount > redBlownUpCount then
self:RewardWin("Blue")
end
self:DisplayContributions()
end
function TeamManager:RewardWin(team)
for _, player in pairs(Players:GetPlayers()) do
local matchPlayedData = player:FindFirstChild("leaderstats"):FindFirstChild("MatchPlayed")
local cashData = player:FindFirstChild("leaderstats"):FindFirstChild("Money")
local expData = player:FindFirstChild("leaderstats"):FindFirstChild("Exp")
if matchPlayedData and cashData and expData then
local contributionData = self.TeamContributions[player.Name]
if contributionData and contributionData.team == team then
matchPlayedData.Value = matchPlayedData.Value + 1
PlayerStats.AddExp(player, 50)
cashData.Value = cashData.Value + 50
expData.Value = expData.Value + 50
contributionData.exp = contributionData.exp + 50
contributionData.coins = contributionData.coins + 50
end
end
end
end
function TeamManager:RewardParticipation(cashReward, expReward, matchPlayedReward)
for _, player in pairs(Players:GetPlayers()) do
local cashData = player:FindFirstChild("leaderstats"):FindFirstChild("Money")
local expData = player:FindFirstChild("leaderstats"):FindFirstChild("Exp")
local matchPlayedData = player:FindFirstChild("leaderstats"):FindFirstChild("MatchPlayed")
if cashData and expData and matchPlayedData then
cashData.Value = cashData.Value + cashReward
expData.Value = expData.Value + expReward
matchPlayedData.Value = matchPlayedReward
local contributionData = self.TeamContributions[player.Name]
if contributionData then
contributionData.exp = contributionData.exp + expReward
contributionData.coins = contributionData.coins + cashReward
end
end
end
end
function TeamManager:DisplayContributions()
local redTeamPlayers = {}
local blueTeamPlayers = {}
for playerName, data in pairs(self.TeamContributions) do
if data.team == "Red" then
table.insert(redTeamPlayers, playerName)
elseif data.team == "Blue" then
table.insert(blueTeamPlayers, playerName)
end
end
for _, player in pairs(Players:GetPlayers()) do
local PlayerGUI = player:FindFirstChild("PlayerGui")
if PlayerGUI then
local EndGameResults = PlayerGUI:WaitForChild("ShopGUI"):WaitForChild("Container"):FindFirstChild("Results")
local TeamFrame = EndGameResults:WaitForChild("Teams")
EndGameResults.Visible = true
local RedTeamUI = TeamFrame:WaitForChild("Red")
local BlueTeamUI = TeamFrame:WaitForChild("Blue")
local RedTemplate = RedTeamUI:WaitForChild("Players"):WaitForChild("Template")
local BlueTemplate = BlueTeamUI:WaitForChild("Players"):WaitForChild("Template")
for _, child in pairs(RedTeamUI.Players:GetChildren()) do
if child.Name ~= "Template" then
child:Destroy()
end
end
for _, child in pairs(BlueTeamUI.Players:GetChildren()) do
if child.Name ~= "Template" then
child:Destroy()
end
end
for _, redPlayerName in pairs(redTeamPlayers) do
local playerContribution = self.TeamContributions[redPlayerName]
if playerContribution then
local clonedRedTemplate = RedTemplate:Clone()
clonedRedTemplate.Name = redPlayerName
clonedRedTemplate:FindFirstChild("Name").Text = redPlayerName
clonedRedTemplate.Visible = true
clonedRedTemplate.Parent = RedTeamUI.Players
clonedRedTemplate.Brick.Text = tostring(playerContribution.bricks)
clonedRedTemplate.Coins.Text = tostring(playerContribution.coins)
clonedRedTemplate.Xp.Text = tostring(playerContribution.exp)
end
end
for _, bluePlayerName in pairs(blueTeamPlayers) do
local playerContribution = self.TeamContributions[bluePlayerName]
if playerContribution then
local clonedBlueTemplate = BlueTemplate:Clone()
clonedBlueTemplate.Name = bluePlayerName
clonedBlueTemplate:FindFirstChild("Name").Text = bluePlayerName
clonedBlueTemplate.Visible = true
clonedBlueTemplate.Parent = BlueTeamUI.Players
clonedBlueTemplate.Brick.Text = tostring(playerContribution.bricks)
clonedBlueTemplate.Coins.Text = tostring(playerContribution.coins)
clonedBlueTemplate.Xp.Text = tostring(playerContribution.exp)
end
end
end
end
end
function TeamManager:HandleExplosion(explosionType, explosionSource, explosionRadius, explosionPower, explosionPlayer)
if explosionType ~= "Rocket" and explosionType ~= "Bomb" then
error("explosionType must be either 'Rocket' or 'Bomb'")
end
local blownUpCount = 0
local explosion = Instance.new("Explosion")
if explosionType == "Rocket" then
if typeof(explosionSource) ~= "Vector3" then
error("explosionSource must be a Vector3 for rocket explosions!")
end
explosion.Position = explosionSource
elseif explosionType == "Bomb" then
if not explosionSource:IsA("BasePart") then
error("explosionSource must be a BasePart for bomb explosions!")
end
explosion.Position = explosionSource.Position
end
explosion.BlastRadius = explosionRadius
explosion.BlastPressure = explosionPower
explosion.Parent = Workspace
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.Hit:Connect(function(part)
if part:IsA("BasePart") and not part.Anchored and part.Name ~= "Handle" then
local blownUpFlag = part:FindFirstChild("BlownUp")
if not blownUpFlag then
blownUpFlag = Instance.new("BoolValue")
blownUpFlag.Name = "BlownUp"
blownUpFlag.Parent = part
game:GetService("Debris"):AddItem(blownUpFlag, 2)
blownUpCount = blownUpCount + 1
end
end
end)
wait(0.5)
self:AddContribution(explosionPlayer, blownUpCount)
return blownUpCount
end
return TeamManager