Hello. I’m working on a “employee of the month” system for my game. The way it works (in theory) is that it fetches the list of players and the value of an IntValue and sorts them by highest to display onto the SurfaceGui I’ve set up. It also sets an ImageLabel and TextLabel to the players headshot and players name.
Well. That’s at least what I think would make it work. Issue is I lack the knowledge for tables and how to set a table with a list of players and the value of the IntValue I’m tracking while also updating it every minute or so. I know that I’d require an Update function that would update Table with an UpdatedTable, but I’m not really able to do that if I can’t even get the first Table going.
I’ve tried searching for solutions but it’s either way too complicated for my use case or just simply not what I’m looking for.
Here’s my code.
local Players = game:GetService("Players")
local ImageLabel = script.Parent
local PlayerName = script.Parent.Parent.PlayerName
local PlayerTable = {}
local function onPlayerAdded(player)
table.insert(PlayerTable, player.UserId)
print("Player Added")
print(PlayerTable)
local playerStatsTest = { -- what i'm looking for, players and Value set into one Table
{"P1", 700},
{"P2", 50},
{"P3", 4},
{"P4", 200}
}
table.sort(playerStatsTest,function(a,b)
return a[2] > b[2]
end)
print(playerStatsTest)
local id = player.UserId
local ThumbType = Enum.ThumbnailType.HeadShot
local ThumbSize = Enum.ThumbnailSize.Size150x150
local plrAvatar, isReady = Players:GetUserThumbnailAsync(id, ThumbType, ThumbSize)
ImageLabel.Image = plrAvatar
PlayerName.Text = player.Name
end
Players.PlayerAdded:Connect(onPlayerAdded)
TLDR: i’m trying to combine a player name and an intvalue table together into one and sort it from highest to lowest and set the highest player to be displayed onto a gui.
Sorry if I’m asking for too much. Any and all help will be greatly appreciated, Thanks.
There’s no reason for me to save this value. This value is merely used to track how many parts a player has created since joining the server. It resets when the player leaves. I’m not sure if I could start saving that value and just overwriting it with 0 everytime the player leaves. Is that a partial solution?
Add all the players to a table and sort it every time their value changes.
Fixed code:
local Players = game:GetService("Players")
local ImageLabel = script.Parent
local PlayerName = script.Parent.Parent.PlayerName
local function ReturnEmployeeOfTheMonth()
local playerTable = {}
for i, player in ipairs(Players:GetPlayers()) do
table.insert(playerTable, {
player.Name,
player.IntValue
})
end
table.sort(playerTable, function(a, b)
return a[2] > b[2]
end)
return playerTable[1]
end
local function onPlayerAdded(player)
local intValue = player.IntValue --//Change name if needed
intValue.Changed:Connect(function()
local employeeOfTheMonth = ReturnEmployeeOfTheMonth()
print(employeeOfTheMonth)
end)
--[[
local id = player.UserId
local ThumbType = Enum.ThumbnailType.HeadShot
local ThumbSize = Enum.ThumbnailSize.Size150x150
local plrAvatar, isReady = Players:GetUserThumbnailAsync(id, ThumbType, ThumbSize)
ImageLabel.Image = plrAvatar
PlayerName.Text = player.Name
]]
end
Players.PlayerAdded:Connect(onPlayerAdded)
local employeeOfTheMonth = ReturnEmployeeOfTheMonth()
print(employeeOfTheMonth)
I need more details like the explorer, what type of script it is, etc, in order to fully fix it.
are you sure it wont bug out because you put something where the second position is tho? im not sure the game will treat it as the position parameter being empty
Thank you so much. One small issue though. How would I go about removing old table entries and/or overwriting a table entry?
Here’s how your (now slightly edited) code looks like:
local Players = game:GetService("Players")
local ImageLabel = script.Parent
local PlayerName = script.Parent.Parent.PlayerName
local playerTable = {}
local function ReturnEmployeeOfTheMonth()
for i, player in ipairs(Players:GetPlayers()) do
table.insert(playerTable, {
player.Name,
player.BrickCount.BrickCountValue.Value
})
end
table.sort(playerTable, function(a, b)
return a[2] > b[2]
end)
return playerTable[1]
end
local function onPlayerAdded(player)
local intValue = player.BrickCount.BrickCountValue --//Change name if needed
intValue.Changed:Connect(function()
local employeeOfTheMonth = ReturnEmployeeOfTheMonth()
print(employeeOfTheMonth)
end)
local id = player.UserId
local ThumbType = Enum.ThumbnailType.HeadShot
local ThumbSize = Enum.ThumbnailSize.Size150x150
local plrAvatar, isReady = Players:GetUserThumbnailAsync(id, ThumbType, ThumbSize)
ImageLabel.Image = plrAvatar
PlayerName.Text = player.Name
end
Players.PlayerAdded:Connect(onPlayerAdded)
while true do -- part i added in
ReturnEmployeeOfTheMonth()
print(playerTable)
task.wait(10)
end
local employeeOfTheMonth = ReturnEmployeeOfTheMonth()
print(employeeOfTheMonth)
The reason it’s player.BrickCount.BrickCountValue is because I’m hiding the IntValue in a seperate folder for it to not show up on the leaderboard.
Here’s screenshots of my explorer in case you need them.
(employee of the month poster)
It’s because you put the playerTable in the wrong scope, it should only be in that local function.
Fixed code:
local Players = game:GetService("Players")
local ImageLabel = script.Parent
local PlayerName = script.Parent.Parent.PlayerName
local function ReturnEmployeeOfTheMonth()
local playerTable = {}
for i, player in ipairs(Players:GetPlayers()) do
table.insert(playerTable, {
player.Name,
player.BrickCount.BrickCountValue.Value
})
end
table.sort(playerTable, function(a, b)
return a[2] > b[2]
end)
return playerTable[1]
end
local function onPlayerAdded(player)
local intValue = player.BrickCount.BrickCountValue --//Change name if needed
intValue.Changed:Connect(function()
local employeeOfTheMonth = ReturnEmployeeOfTheMonth()
print(employeeOfTheMonth)
end)
local id = player.UserId
local ThumbType = Enum.ThumbnailType.HeadShot
local ThumbSize = Enum.ThumbnailSize.Size150x150
local plrAvatar, isReady = Players:GetUserThumbnailAsync(id, ThumbType, ThumbSize)
ImageLabel.Image = plrAvatar
PlayerName.Text = player.Name
end
Players.PlayerAdded:Connect(onPlayerAdded)
while true do -- part i added in
local employeeOfTheMonthData = ReturnEmployeeOfTheMonth()
print("Player: ".. employeeOfTheMonthData[1])
print("Amount earned: ".. employeeOfTheMonthData[2])
task.wait(10)
end
local employeeOfTheMonth = ReturnEmployeeOfTheMonth()
print(employeeOfTheMonth)