So i’m creating a Points System basically it creates a new element for each player inside this Table
local FFAPoints = {}
for _, P in pairs (Players:GetPlayers()) do
table.insert(FFAPoints,P.Name)
FFAPoints[P.Name] = 0
end
however how would I go about returning the player with the highest amount of points? I’ve tried different methods and nothing seems to be working for me.
function HighestPoints()
local highest = 0
local owner
for i,player in pairs(game.Players:GetPlayers()) do
if FFAPoints[player.Name] > highest then
highest = FFAPoints[player.Name]
owner = player
end
end
return owner
end
--Points : bob(1) sam(2) tom(5)
local playerwithhighest = HighestPoints()
print(playerwithhighest.Name) --tom
Yes, but like the guy said if you’re using leaderstats and In this case i’m not using leaderstats. Can you try making this but in this case with tables
Also I suggest not doing it in the way you have, If you were to keep track of player data per round I suggest doing something like this:
local PlayerData = {}
for _, P in next, game.Players:GetChildren() do
PlayerData[P.Name] = 0
end
then to check you can simply do this
local HighestName , HighestScore = nil, 0
for Name, Score in next, PlayerData do
if Score > HighestScore then
HighestScore = Score
HighestName = Name
end
end
print(HighestName, HighestScore)
local FFAPoints = {}
for _, P in next, Players:GetChildren() do
table.insert(FFAPoints,P.Name)
FFAPoints[P.Name] = 0
end
function GetHighestPoints()
local HighestName, HighestPoints = nil, 0
for Name, Points in next, FFAPoints do
if Points > HighestPoints then -- This is where it error Points is apparently a string I tried doing tonumber but that just sets it as nil
HighestPoints = Points
HighestName = Name
end
end
return HighestName, HighestPoints
end
local PlayerHighestPoints = GetHighestPoints()
print(PlayerHighestPoints)
local FFAPoints = {}
for _, Player in pairs (game.Players:GetPlayers()) do
local New = {
["Player"] = Player,
["Points"] = 0
}
table.insert(FFAPoints, New)
end
table.sort(FFAPoints, function(a, b)
return a.Points > b.Points
end)
print(FFAPoints[1].Player.Name.." has the most amount of points with: "..FFAPoints[1].Points)
Your table FFAPoints was a array of player names and then you added dictionary entries. The error was because of an iteration over the array part where the key was an integer and the value a player name. Instead do something like kylerzong posted.
I just made a quick function to get the highest value in a dictionary or a table. If with dictionary, it returns the key and the value while with array, only the value.
local t = {
5,
2,
1,
2,
1
}
local function getGreatestValue(table)
-- If table is a array
if (table[1]) then
local highest
for i = 1, #table do
if not (highest) then
highest = table[i]
else
if (table[i] > highest) then
highest = table[i]
end
end
end
return highest
-- table is a dictionary
else
local key, highest
for k, v in pairs(t) do
if (key and highest) then
if (v > highest) then
key, highest = k, v
end
else
key, highest = k, v
end
end
return key, highest
end
end
print(getGreatestValue(t))
It shouldnt be running in a loop fast cause when you add a player twice it will append instead of overwriting, so you would need to clear the table then re-add the players if for some reason thats what you wanted to do
Maybe this is better for what you are trying to do:
local FFAPoints = {}
for _, Player in pairs(game.Players:GetPlayers()) do
FFAPoints[Player.Name] = 0
end
FFAPoints["TestPlayer1"] = 100
FFAPoints["TestPlayer2"] = 9
FFAPoints["TestPlayer3"] = 5
FFAPoints["TestPlayer4"] = 199
FFAPoints["TestPlayer5"] = 200
function GetHighestScorer(FFAPointsTable)
local HighestScorer = {
["Points"] = nil,
["Username"] = nil,
}
for Username, Points in pairs(FFAPointsTable) do
if HighestScorer.Points == nil then
HighestScorer.Points = Points
HighestScorer.Username = Username
elseif HighestScorer.Points < Points then
HighestScorer.Points = Points
HighestScorer.Username = Username
end
end
return HighestScorer
end
local Data = GetHighestScorer(FFAPoints)
print(Data.Username.." has the most points with: "..Data.Points)