Script only working for one person

Hello, this might be a bit confusing because of the way I have certain things named, so read carefully.

I have a script where whenever someone gets a kill, their leaderstats for “kills” goes up by 1. And their deaths go up by 2. (I wanted to rename this to points but when I did it stopped working so I am just keeping it like this for now.)

I then have it where whenever they die, their deaths go back to 0.

The problem is that it only works with 1 player, both players can kill each other, and the script will work fine except for when 1 of the players die, it does not set their deaths to 0. This only happens with one of the players, as when you kill the other player it does get set back to 0.

Sorry if this is a bit confusing. I am not much of a scripter. I can show you it in the game if you’d like.

Oh geez almighty

Hm, alright the first thing I notice are the variable warnings, you should be a local variable instead :thinking:

Also, why are you changing the Deaths.Value to 0? (Aka subtracting the Deaths.Value from itself)

Next, please don’t use the second parameter when using Instance.new(), it’s slower

This should work, but I’m not exact if it will:

game.Players.PlayerAdded:Connect(function(Player)
    local Stat = Instance.new("IntValue")
    Stat.Name = "leaderstats"
    Stat.Parent = Player
  
    local Kills = Instance.new("IntValue")
    Kills.Name = "Kills"
    Kills.Parent = Stat

    local Deaths = Instance.new("IntValue")
    Deaths.Name = "Deaths"
    Deaths.Parent = Stat

    if Use_DP then --This is either not a valid variable or you didn't define it right, not enough information
        loadStat(player, "Kills", Kills)
        loadStat(player, "Deaths", Deaths)
    end

    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")

        Humanoid.Died:Connect(function()
            Deaths.Value = 0
            local Cre = Humanoid:FindFirstChild("creator")
            if Cre and Cre.Value then

               local leaderstats = Cre.Value:FindFirstChild("leaderstats")
               if leaderstats then
                  leaderstats.Kills.Value += 1
                  leaderstats.Deaths.Value += 2 --Wait why doe
               else
                  Kills.Value = Kills.Value
               end
               if Use_DP then
                   saveStat(Cre.Value, leaderstats.Kills.Value, "Kills")
                   saveStat(Player, Kills.Value, "Kills")
               end
            end
        end)
    end)
end)

For some reason thats not changing any of the leader stats. The reason I was wanting deaths to go up by 2 whenever you get a kill is because I am gonna rename it to points for example. (Sorry I know that was confusing). I then wanted the “points” to reset back to 0 once you die. I might be doing something wrong but when using that code nothing happens when you get a kill or die.

Are you getting anything from your Output screen at all?

I can’t really check, unless someone joins the studio with me and we go in team create, so I can see what outputs when you get a kill or die.

Hm I edited it a bit, could you try again and see if anything changes?

They still don’t change. But also, the Use_DP variable was something that was defined earlier in the code, but I don’t need it. It can be removed. I don’t think that would make a difference though as I removed it and it still didn’t work, unless I did something wrong.

Ok I think I was stupid all this time

There isn’t really much I can do apart from trying it once more, I believe I have it all fixed

If you still get the same outcome then maybe you could try it on a separate place & add print() statements to check if everything’s working

It works! Thank you so much, you were very helpful.

1 Like

I definitely did not get confused by how the Death values got implemented Np! Do make sure to change that though when you can :sweat_smile:

1 Like