Basically, in this script, everytime a death occurs in a certain team, the teams death value will increase by 1. This is represenetd with the RedDeaths and BlueDeaths variables below, being 2 seperate teams. RedDeaths being the IntValue of deaths of the red team and same for blue, just with a different name. Everything seems to work, except, when a player dies on one of these teams, it prints both if statements, as if the player were on both teams at once when he/she died. For example, if i was on the blue team and i died, it would print both that i died on the red and blue team. Does anyone know how i could fix this error in my script? Thanks for your time! ;)
Script:
local Player = game.Players.LocalPlayer local humanoid = Player.Character:findFirstChild("Humanoid") local RedTeamPlayers = game.Teams.Red:GetPlayers() local BlueTeamPlayers = game.Teams.Blue:GetPlayers() local RedDeaths = game.Workspace.GameScript.DeathMatchMode.Config.RedDeaths local BlueDeaths = game.Workspace.GameScript.DeathMatchMode.Config.BlueDeaths function DiedX_X(BlueTeamPlayers) BlueDeaths.Value = BlueDeaths.Value + 1 end humanoid.Died:connect(DiedX_X) if BlueDeaths.Value == 1 then print('1 Blue Death') end function DiedX_X(RedTeamPlayers) RedDeaths.Value = RedDeaths.Value + 1 end humanoid.Died:connect(DiedX_X) if RedDeaths.Value == 1 then print('1 Red Death') end
You have tied both DiedX_X
functions to the Died event. So, of course, both will fire upon your character's death.
What you need to do is have one function tied to the Died event, and check the team inside of that function.
Also, this should really be handled on the Server. An easy way to implement this to every player would be using the PlayerAdded
and CharacterAdded
events.
The following should be a Script in ServerScriptStorage, or Workspace.
local configs = workspace.GameScript.DeathMatchMode.Config; local RedDeaths = configs.RedDeaths; local BlueDeaths = configs.BlueDeaths; game.Players.PlayerAdded:connect(function(plr) plr.CharacterAdded:connect(function(char) local hum = char:WaitForChild("Humanoid"); local team = plr.Team; local stat = (team == game.Teams.Red and RedDeaths) or BlueDeaths; hum.Died:Connect(function() stat.Value = stat.Value + 1; print(stat.Name.." has been incremented"); end) end end