Hello, could someone help me to fix this issue with my round handler?
-
What do you want to achieve? This part of the script is intended to find the player who won and how many points they had,
-
What is the issue? The winner is ALWAYS being set to the person who joined first, regardless of points.
-
What solutions have you tried so far? I tested with my friend and determined that there is an issue with highestnumber. I am unsure if the tables are being set wrong or if the variables from the tables are being read wrong.
Relevant code:
local timer = game.Workspace.TimerSign.SurfaceGui.TimerLabel
local leaderboard = game.Workspace.Leaderboard.SurfaceGui.TextLabel
local players = game:GetService("Players")
local storage = game.ReplicatedStorage
local servstorage = game.ServerStorage
--The above is from the start of the code
--Tables
local playertable = {}
local player_results = {}
--Make functions
local function getResults()
for i, v_player in ipairs(players:GetPlayers()) do
playertable[i] = v_player.Name
player_results[i] = v_player.leaderstats.Points.Value
v_player.leaderstats.Points.Value = 0
end
end
local function calculateResults()
local highestnumber = -1 --Set highest number
for i, v in pairs(player_results) do --Find index of highest number in results
if v > highestnumber then
highestnumber = v
end
local indexfound = table.find(player_results, highestnumber)
return indexfound
end
end
--IndexFound returning 1 always? (highestnumber not setting properly)
--Run functions
getResults()
local indexfound = calculateResults() --Get indexfound
local winningplayer = playertable[indexfound] --Use indexfound to find winning player
local highestnumber = player_results[indexfound] --Use indexfound to find highest score
leaderboard.Text = "#".. tostring(indexfound) ..", ".. tostring(highestnumber)
players[winningplayer].leaderstats.Wins.Value += 1
storage.ShowResults:FireAllClients(winningplayer, highestnumber)
local playertable = {}
local player_results = {}
Thank you for looking at this issue!