How to stop wins being added twice

Hi I am trying to make a 1v1 system and I am having a problem with the wins when the player has left it gives the win but when a new player joins, when a new match starts and a player has scored 3 goals then it gives them 2 wins or even 3 at some point, I have noticed the game is starting and I think the intermission is starting twice when 3 goals is scored it fires the intermission event.


local function GiveTeamWinsOnPlayerLeaving(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has won the game")
			player.stats.Wins.Value += Amount
			print("Gave player win")
			break
		end
	end
end

local function GiveTeamWins(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has won the game")
			player.stats.Wins.Value += Amount
			print("Gave player win")
		end
	end
end

local function GiveTeamWinsOnPlayerLeaving(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has won the game")
			player.stats.Wins.Value += Amount
			print("Gave player win")
			break
		end
	end
end

local function GiveTeamWins(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has won the game")
			player.stats.Wins.Value += Amount
			print("Gave player win")
		end
	end
end

local function StartingGame()
	print("Starting Game")
	Ball.Position = Vector3.new(221.21, 1.944, -437.77)
	Ball.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	Ball.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
	HomeGoalTrigger.Parent = workspace
	AwayGoalTrigger.Parent = workspace
	inGame.Value = true
	InIntermission.Value = false
	Status.Value = "In match"
	while true do
		task.wait()
		if HomeScore.Value == 3 then -- If home has scored 3 goals
			GiveTeamWins("Green", 1)
			Status.Value = "Green team wins the game"
			print("3 goals have been scored")
			wait(2)
			inGame.Value = false
			script.Parent.Intermission:Fire()
			break
		end
		if AwayScore.Value == 3 then
			GiveTeamWins("Red", 1)
			Status.Value = "Red wins the game"
			print("3 goals have been scored")
			wait(2)
			inGame.Value = false
			script.Parent.Intermission:Fire()
			break
		end
	end
end

script.Parent.StartGame.Event:Connect(StartingGame)

if InIntermission.Value == true then
	print("In intermission")
	script.Parent.Intermission:Fire()
end

if inGame.Value == true then
	print(#plrs)
	script.Parent.StartGame:Fire()
	script.Parent.TeleportTeamPlayers:Fire()
end

HomeGoalTrigger.Touched:Connect(function(hit)
	if hit.Name == "TPS" then
		script.Parent.TeleportTeamPlayers:Fire()
		task.wait(0.25)
		AwayScore.Value = AwayScore.Value + 1
		hit.Position = Vector3.new(221.21, 1.944, -437.77)
		hit.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		hit.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	end
end)

AwayGoalTrigger.Touched:Connect(function(hit)
	if hit.Name == "TPS" then
		script.Parent.TeleportTeamPlayers:Fire()
		task.wait(0.25)
		HomeScore.Value = HomeScore.Value + 1
		hit.Position = Vector3.new(221.21, 1.944, -437.77)
		hit.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		hit.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	end
end)

game.Players.PlayerRemoving:Connect(function(Plrs)
	print("Player has left the game data has been removed.")
	if Plrs.Team == game.Teams.Red and inGame.Value == true and AwayScore.Value > HomeScore.Value then
		script.Parent.NotEnoughPlayers:Fire()
		script.Parent.Intermission:Fire()
	elseif Plrs.Team == game.Teams.Red and AwayScore.Value <= HomeScore.Value then
		script.Parent.NotEnoughPlayers:Fire()
		script.Parent.Intermission:Fire()
		GiveTeamWinsOnPlayerLeaving("Green", 1)
	end
	if Plrs.Team == game.Teams.Red and inGame.Value == true and HomeScore.Value > AwayScore.Value then
		script.Parent.NotEnoughPlayers:Fire()
		script.Parent.Intermission:Fire()
	elseif Plrs.Team == game.Teams.Green and inGame.Value == true and HomeScore.Value <= AwayScore.Value then
		script.Parent.NotEnoughPlayers:Fire()
		script.Parent.Intermission:Fire()
		GiveTeamWinsOnPlayerLeaving("Red", 1)
	end
end)

I tried adding debounce but I don’t know if I denounced it correctly then it didn’t work so I undo the code.

Any help is appreciated.

I think I have got somewhere now, I looked up the article on debounce and have got it nearly working just need to fix something

Use elseif instead of an entirely new code block.

Your code:

if foo then
    print('bar')
end
if foo then
    print('bar')
end

Optimized code:

if foo then
    print('bar')
elseif foo then
    print('bar')
end

Although they are identical, the second one will only print once.

1 Like

How many times does the “Gave player win” message show in the output? Also, I’m not sure if this could be the issue, but you have 2 duplicated functions for GiveTeamWins and GiveTeamWInsOnPlayerLeaving.

No, it gives the wins correctly when leaving the game the issue is it is firing intermission twice I think

I added debounce but it takes after the first game when the player has rejoined for the debounce to work pretty sure the game isn’t stopping unless the score is 3 goals will need to fix

Unsure if you saw but I have provided help to you previously in which you may have missed @ How to stop wins being added twice - #3 by 04nv.

I have fixed it plus I have added your code at points of the script which will help my game be more optimized thank you for the help i fixed it by adding debounce