I have this script that I’m trying to make where it will go through all the players who are on the “Playing” team, find the NumberValue called “RoundKills” look at the value, and then find the highest value out of all the players, and then keep the highest value as a variable (Keeping the player who has the most, and then how many then had)
(serverscript in serverscriptservice)
(this is just a part of a bigger script , 110 lines)
for i = 1, #Players do
if players[i].Team == game:GetService("Teams")["Playing"] then
--find the highest value and keep the player as a variable
end
end
This is the script for the value I’m looking to find
(localscript in starterplayerscripts)
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
local KillNum = Instance.new("NumberValue")
KillNum.Name = "RoundKills"
KillNum.Value = 0
KillNum.Parent = player.Character
end)
Not a good Idea to Parent the Players Score to their Character, because when they die, their Character would be reset, thus deleting the score, plus, the Character does not exist when the Player spawns in.
So you would look through every Player, and Have a Variable to Keep track of the Player, and The Score, like so:
local CurrentValue = -math.huge -- so we can check if a Value is greater than a certain amount
local CurrentPlayer -- to store the Player that holds the value
for i,player in game.Players:GetPlayers() do -- iterates through all Players in the game
local KillVal = player:FindFirstChild("RoundKills") -- looks for Item
if KillVal and KillVal.Value > CurrentValue then -- if the number is greater than the old
CurrentValue = KillVal.Value -- new currentValue
CurrentPlayer = player -- new CurrentPlayer
end
end
return {CurrentPlayer, CurrentValue} -- returns the Data
I moved it to be just in the player now, and also is there a way to instead of searching every player, rather just every player who is on a specific team?
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
local KillNum = Instance.new("NumberValue")
KillNum.Name = "RoundKills"
KillNum.Value = 0
KillNum.Parent = player
end)
I think this will work
for i ,player in game.Teams.Playing:GetPlayers() do
local KillVal = player:FindFirstChild("RoundKills")
if KillVal and KillVal.Value > CurrentValue then
CurrentValue = KillVal.Value
CurrentPlayer = player
end
end
How would I then later reference to the player who had the most kills , as if I wanted to show their name and how many in a TextLabel (i already have a way to show it, im just looking for how to find which player it was with most kills )