Change BoolValue True/False on who has the highest kills

Hello! I need help at this because I am not too advance and I’ve tried working on many scripts for this crap. I need help with making a script that set’s all players BoolValue that’s called “Lead” and who ever has the highest kills on the classic leaderboard would get their BoolValue set to true while everyone’s false.

This is so that right when the Round is over it checks who has their value set to true and give them a win and all of their stats just resets.

Using pseudo code:

PSEUDO CODE

IDENTIFY A TABLE

EVENT THAT IS FIRED AFTER EVERY KILL (PLAYERID)
     CHECK IF TABLE HAS KEY OF PLAYERID
        RAISE TABLE[PLAYERID] BY ONE
    ELSE
        CREATE VALUE OF TABLE[PLAYERID] EQUAL TO ONE
END

The PLAYERID parameter is the person who got the kill.

Why do you need a BoolValue for each player? Can’t you just use a ModuleScript to get the best player?

local module = {}

function module.getBestPlr()
    local highest = {plr = nil,value = 0}

    for _, p in game.Players:GetPlayers() do
        if p.leaderstats.Kills.Value > highest.value then
            highest.value = p.leaderstats.Kills.Value
            highest.plr = p
        end
    end
end

return module
-- in another script

local module = require(game.ReplicatedStorage.ModuleScript)

local best = module.getBestPlr()

print(best.plr,best.value)

The reason why I am doing this way it’s because I am using an older system for this round thing but It’s really good. I am just replacing it because the way it worked was the people who are still in the round has it enabled but I am trying to replace it only a player with the most kills will have it Enabled.

1 Like

This is the older system script.

image

Ok, I get what you mean. Something like this should work.

local module = {}

function module.getBestPlr()
    local highest = {plr = nil,value = 0}

    for _, p in game.Players:GetPlayers() do
        if p.leaderstats.Kills.Value > highest.value then
            highest.value = p.leaderstats.Kills.Value
            highest.plr = p
        end
    end

    if highest.plr then
        highest.plr.Lead.Value = true
    end
end

return module
1 Like

I’ll let you know if that worked.

Hey, For some reason the value isn’t updated as true. There’s no error or anything like that for some reason.

image
image

This won’t work because plr and value would both be nil and 0 because it has not been set yet. If I were to replace that code, I would compile all kill values with their indexes being their player names or another setup could be an array that has this: {"John_Doe", 9}. Then, it’s just simply compare and find the highest value. I hope you understand what I said.

Thanks but I sort of understood on what you mean by, I am not that advanced when it comes to this so I am still confused on how I should make a script like this.

This is the only thing I need it to be done and the rest would be easy for me to set it up.

This example should give you an idea:

local List = {}

local HighestValue = 0
local PlayerWithTheHighestKills = ""

for _, Player in pairs(Players:GetPlayers()) do
    List[Player.Name] = Player.leaderstats.Kills.Value
end

-- Get the highest value
for Name, Value in pairs(Value) do
    if HighestValue < Value then
       HighestValue = Value
       PlayerWithTheHighestKills = Name
    end
end

print(PlayerWithHighestKills)

A clearer example can be seen here (typescript):
https://roblox-ts.com/playground/#code/JYWwDg9gTgLgBAbwAoBsCGBPAplAzgXzgDMoIQ4AiAASgCMAPGXAelxwDdgBjLXCgWABQQrhAB2ueABlgkuAF44YrAHc4AWTRgAPJKjAxAcwA0SgK4haOAHwAKAJRChKLPAASwQwAteMAGpoKGZYCnAADM6ucKiYOADqwDBeACo+Ht6+ANLAKCi4oRQCwoIx2HgAdADirqU4uA7lRNAAomhcXra2tVD2CtaIQgCQohLwLmgAJnUwaEyh3eUAYgYTy3gwAMJeORO2FONT67N8joLD4nLZufmKB9PHAPxLK2uSWzt7V3kUp0PARHBbF98gAyEFwYHlACSuAAgnsAHIWKxQAJBLA-XoIIaDGSScpsGBddBlcoItAgLCmSFo4KnQb4IT4X6CJpQQEjOQAbXJlNMtKwAF04BAAWA0MA8LY8TB7Fi-gDbOkfJIBXBtHABfKzoNlb41YoBTjugkkqksHrJMDQrysENCFg8iFsTqRjADMF7UynIIwPoxESTYkUmlPCqYMD7EA
But to put it simply, use a variable or a state value to check who is the player with the highest amount of kills.