What do i want to achieve? I want that at the end of a round there is a frame with the name of the player with the hightest round points (an intvalue under each player). Basically i found this script and i modificated it, but the problem is that every 5 seconds (bc for now i put while wait(5) do) the textlabel with the name of the player of the hightest value change with random players (watch the video for understand more)
local DataModel = game
local Players = DataModel:GetService("Players")
local function RankPlayers()
local RankedPlayers = {}
for _, Player in ipairs(Players:GetPlayers()) do
local Stat = Player:FindFirstChild("SpainPoints") --Change to name of stat.
if Stat then
table.insert(RankedPlayers, {Player, Stat.Value})
end
end
table.sort(RankedPlayers, function(Left, Right)
if Left[2] < Right[2] then
return Right
end
end)
for _, PlayerArray in pairs(RankedPlayers) do
local Player = PlayerArray[1]
local StatValue = PlayerArray[2]
print(Player.Name, StatValue)
game.ReplicatedStorage.SPIN.Value = "The winner is ".. Player.Name.." with "..StatValue.." Points"
if Player then
local userid = Player.UserId
local thumtype = Enum.ThumbnailType.HeadShot
local thumSize = Enum.ThumbnailSize.Size420x420
local content = game.Players:GetUserThumbnailAsync(userid, thumtype, thumSize)
game.ReplicatedStorage.SPINimage.Value = content
game.ReplicatedStorage.GoSpainGUIvalue:FireAllClients()
local cloned = game.ReplicatedStorage.PointsGiver:Clone()
cloned.TextLabel.Text = "GG! You finished first and won 30 points!"
cloned.Parent = Player.PlayerGui
cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.834, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 30
wait(4)
cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.99, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
wait(1)
cloned:Destroy()
---
end
end
return RankedPlayers
end
while wait(5) do
RankPlayers()
end
sry if you don’t understand,
all answers are welcome
i took the @Forummer script
Ty for the answer, but it is not working, same error
local DataModel = game
local Players = DataModel:GetService("Players")
local function RankPlayers()
local RankedPlayers = {}
for _, Player in ipairs(Players:GetPlayers()) do
local Stat = Player:FindFirstChild("SpainPoints") --Change to name of stat.
if Stat then
table.insert(RankedPlayers, {Player, Stat.Value})
end
end
table.sort(RankedPlayers, function(a, b)
return a[2] < b[2]
end)
for _, PlayerArray in pairs(RankedPlayers) do
local Player = PlayerArray[1]
local StatValue = PlayerArray[2]
print(Player.Name, StatValue)
game.ReplicatedStorage.SPIN.Value = "The winner is ".. Player.Name.." with "..StatValue.." Points"
if Player then
local userid = Player.UserId
local thumtype = Enum.ThumbnailType.HeadShot
local thumSize = Enum.ThumbnailSize.Size420x420
local content = game.Players:GetUserThumbnailAsync(userid, thumtype, thumSize)
game.ReplicatedStorage.SPINimage.Value = content
game.ReplicatedStorage.GoSpainGUIvalue:FireAllClients()
local cloned = game.ReplicatedStorage.PointsGiver:Clone()
cloned.TextLabel.Text = "GG! You finished first and won 30 points!"
cloned.Parent = Player.PlayerGui
cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.834, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 30
wait(4)
cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.99, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
wait(1)
cloned:Destroy()
---
end
end
return RankedPlayers
end
while wait(5) do
RankPlayers()
end
Basically every 5 seconds the player in the textlabel change. For example if player1 has 5 points and player2 has 0 then the textlabel show
every 5 seconds a random different player, player1 and player2 every 5 seconds (bc of the while wwait(5) do). I want that it shows ONLY the player with the hightest value *
function GetPlayerWithHighestScore() -- Name is unreasonably long, cant think of a better one
local plrs = {}
for _, p in game.Players:GetPlayers() do
local l = p:FindFirstChild("leaderstats")
if l then
local stat = l:WaitForChild("statname")
plrs[p] = stat
end
end
local best = {
plr = nil;
val = 0:
}
for p, val in plrs do
if val > best.val then
best.plr = p
best.val = val
end
end
return best.plr,best.val
end
local bestplr,val = GetPlayerWithHighestScore()
If this doesn’t work, then tell me the error because I wrote this on mobile.
function GetPlayerWithHighestScore()
local plrs = {}
for _, p in game.Players:GetPlayers() do
local stat = p:WaitForChild("SpainPoints")
if stat then
plrs[p] = stat
end
end
local best = {
plr = nil;
val = 0
}
for p, val in plrs do
if val > best.val then
best.plr = p
best.val = val
end
end
return best.plr,best.val
end
local bestplr,val = GetPlayerWithHighestScore()
while wait(2) do
print(bestplr, val)
end```
function GetPlayerWithHighestScore()
local plrs = {}
for _, p in ipairs(game.Players:GetPlayers() ) do
local stat = p:FindFirstChild("SpainPoints")
if stat then
plrs[p] = stat
end
end
local best = {
plr = nil;
val = 0
}
for p, val in plrs do
print(val)
if val > best.val then
best.plr = p
best.val = val
end
end
return best.plr,best.val
end
local bestplr,val = GetPlayerWithHighestScore()
Ok, I rewrote the script to use UserIDs instead of Player Objects
function GetPlayerWithHighestScore() -- Name is unreasonably long, cant think of a better one
local plrs = {}
for _, p in game.Players:GetPlayers() do
local l = p:FindFirstChild("leaderstats")
if l then
local stat = l:WaitForChild("statname")
plrs[p.UserId] = stat
end
end
local best = {
id = 1;
val = "If it prints this, then that means it didnt work.";
}
for id, val in plrs do
if val > best.val then
best.id = id
best.val = val
end
end
return game.Players:GetPlayerByUserId(best.id),best.val
end
local bestplr,val = GetPlayerWithHighestScore()
function GetPlayerWithHighestScore() – Name is unreasonably long, cant think of a better one
local plrs = {}
for _, p in game.Players:GetPlayers() do
local stat = p:WaitForChild("SpainPoints")
if stat then
plrs[p.UserId] = stat
end
end
local best = {
id = 0;
val = "If it prints this, then that means it didnt work.";
}
for id, val in plrs do
if val > best.val then
best.id = id
best.val = val
end
end
return game.Players:GetPlayerByUserId(best.id),best.val