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.