Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this script continue to print 2 values?

Asked by 7 years ago
Edited 7 years ago

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

1
It may be because both of the function names are "DiedX_X" so it's calling both functions. Mayk728 855 — 7y
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 Seabiscuitz 21 — 7y
0
Have you tried changing one of the "DiedX_X"'s to a different name? I'm most certain that's the error Mayk728 855 — 7y
0
Okay i just tested and that wasn't the problem. Sorry bout that. Mayk728 855 — 7y
0
You're fine ;) Seabiscuitz 21 — 7y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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
0
Goulstem, that didnt seem to work, soooo...... Seabiscuitz 21 — 7y
0
What are the actual Team names in your game. I sort of guessed on line 9. Goulstem 8144 — 7y
Ad

Answer this question