Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this script continue to print 2 values?

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

5 comments

1

It may be because both of the function names are "DiedX_X" so it's calling both functions.

0

Mayk728 That is true, but i need two separate DiedX_X functions to differentiate the death of a Blue Team Player and a Red Team Player

0

Have you tried changing one of the "DiedX_X"'s to a different name? I'm most certain that's the error

0

Okay i just tested and that wasn't the problem. Sorry bout that.

0

You're fine ;)

1 answer

✓ Accepted answer

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

2 comments

0

Goulstem, that didnt seem to work, soooo......

0

What are the actual Team names in your game. I sort of guessed on line 9.

This discussion is preserved as it was. New answers, comments, and votes are closed.