Right now I am attempting to make a system that gives cash (or in my game, "Explode Points") to the player with the best KO to wipeout ratio at the end of every round. The problem is, I have absolutely no idea how to do that. So far I made a weird little script that makes values in a folder that I'm going to try and take the highest value from:
local function findWinner()
local children = game.Players:GetChildren()
for _, child in pairs(children) do
local stats = child:WaitForChild("leaderstats")
if stats then --making values
local KOs = stats.KOs
local Deaths = stats.Wipeouts
local newValue = (KOs.Value / Deaths.Value)
local PnewValue = Instance.new("IntValue", game.ReplicatedStorage.WinnerValues)
PnewValue.Name = child.Name
PnewValue.Value = newValue
wait(10)
KOs.Value = 0
Deaths.Value = 0
PnewValue:Destroy()
end
end
local winnerFolder = game.ReplicatedStorage.WinnerValues
end
I hope I didn't break the rules by not providing any code for finding the value, but the other questions on this site just didn't make any sense to me or work at all. So how do you go about doing this? (In case you need it, the function is fired every 120 seconds)
Edit: Be sure to tell me if there's a more efficient way to track all of the stats than putting them in a folder, because I'm sure there is but I honestly have no idea
Edit2ElectricBoogaloo: After hours of trying, I still haven't gotten it to work so far. Both answers give me a similar error where the winner is returned as a nil value, and there isn't a system for ties.
Here's my new script where I mostly used the second answer but used some aspects from the one I originally thought answered my question:
local function FindWinner()
local children = game.Players:GetPlayers()
local highestnumber = -10000
local winner = nil
local playerCountChildren = game.Players:GetPlayers();
local playerCount = #playerCountChildren;
for _, child in pairs(children) do
if child:FindFirstChild('leaderstats') then
local stats = child.leaderstats
local KOs = stats.KOs
local Deaths = stats.Wipeouts
local newValue = (KOs.Value / Deaths.Value)
if Deaths.Value == 0 and KOs.Value ~= 0 then
newValue = KOs.Value
end
if newValue > highestnumber then
highestnumber = newValue
winner = child
end
if playerCount == 1 then
winner = child
highestnumber = newValue
end
end
end
return winner, highestnumber
end
Here is the loop I made at the bottom, in case I'm utilizing it wrong:
while true do
for a=120,1,-1 do
print(a)
wait(1)
end
print ('0')
setupMap()
local winner, highestnumber = FindWinner()
print (winner.Name.. " is the winner with a KDR ratio of " ..highestnumber)
local message = Instance.new('Message',game.Workspace)
message.Text = (winner.Name.. " is the winner with a Kill Death Ratio of " ..highestnumber .."!")
local winPlayer = game.Players:FindFirstChild(winner.Name)
if winPlayer then
local winStats = winPlayer:FindFirstChild("leaderstats")
if winStats then
local winCash = winStats:FindFirstChild("ExplodePoints")
winCash.Value = winCash.Value + 500
wait(5)
end
end
resetValues()
message:Destroy()
wait()
end

1 comment
The errors are in the loop, saying that i'm trying to compare to a nil value (the winner value, during the print)