Ok, ok. Before I start, yes I know there are a lot of posts regarding this but unfortunately I’m unable to understand those.
Hey there all! So, I’m making an FPS game, and I’m kinda stuck at this part. I don’t know how to get the player with the highest amount of kills. Any type of help will be appreciated, thanks!
Extra Info:
- Status is a StringValue inside a folder named ‘Values’ in ReplicatedStorage.
- CurrentGamemode is another StringValue in the same place as Status.
- PlayerContainer is a separate folder in ReplicatedStorage in which, when a player clicks “Deploy” he’ll spawn in the game and a StringValue named as the player will be added to the folder.
Code
Status.Changed:Connect(function(NewVal)
if NewVal == "Round" then
CurrentGamemode.Changed:Connect(function(NewVal2)
if NewVal2 == "Deathmatch" then
local plrs = {}
local function OnPlayerContainerAdded(child)
if Players:FindFirstChild(child.Name) then
local FoundPlayer = Players:FindFirstChild(child.Name)
table.insert(plrs, {
["Player"] = FoundPlayer;
["Kills"] = FoundPlayer:WaitForChild("NonSaveInfo"):WaitForChild("Kills")
})
print(plrs[1]["Player"].Name)
end
end
PlayerContainer.ChildAdded:Connect(OnPlayerContainerAdded)
for _, v in ipairs(PlayerContainer:GetChildren()) do
OnPlayerContainerAdded(v)
end
local Highest = nil
table.sort(plrs, function(a, b)
return a["Kills"].Value > b["Kills"].Value
end)
for _, v in pairs(plrs) do
v["Kills"].Changed:Connect(function(x)
print(tostring(x))
Highest = v["Kills"].Value
if x == 20 then
print(string.format(("%s has won the game!"), v["Player"].Name))
end
end)
end
end
end)
end
end)