Scripting issue

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
1 Like

The issue happens because you’re resetting Player 1’s brick count whenever Player 2 destroys bricks, causing an endless loop. You need to make sure each player’s actions are independent and not reset when another player does something.

I don’t see the exact issue in the code, so It may be elsewhere unless the TeamManager:SetupPlayerTeam() function runs for some reason.
What I suspect is that some code somewhere is setting the player 1’s brick count to 0.

Is there anything that might do this? If this is not the case it could be anything from a replication issue to other players sharing some data somewhere unintentionally. I would like to see what happens with 3 players, that might shine some light onto what happens, weather all three reset each other’s data or just one.

Didn’t work for me, attempted it like you mentioned but nothing.

I tried that too and still no luck, not sure what now.

Without seeing more of how the game is structured, I don’t know what is going on. For instance it could be a weird client server desync (depending on what script is running this), or some random piece of code in an unrelated script is screwing things over for some reason.

I have experience with fixing bugs like this, and the problems often come from the most random and unrelated lines of code. It will take long and it will be frustrating, but don’t give up.

I encourage you to look at every thing that might affect .bricks and then narrow it down from there. I have two strategies that I use: using find/replace all window to see everything that uses and affects this piece of code (simply putting .bricks into the search bar), or the more aggressive strategy of turning off scripts and/or functions to see what turns off the problem. The code must be doing this, turning off the code must stop this.
I will try to help in any way I can, good luck.

Yeah, my script is flawless I’m not sure what else I should do.