Alright, this is supposed to go get a value in a gui in all players, and set it in a table, then go through the table and take out every number except the highest number, and then clone the players head with the highest number, righ now its either not removing the numbers from the table or my logic is wrong, the updated version of the code has been edited on this please help
numtble = {} function Check() repeat wait() until game.Players.NumPlayers >= 2 for i,v in pairs(game.Players:GetPlayers()) do table.insert(numtble,v.PlayerGui.Points.Posy.Numbers.Value) wait() print("Awesome2") wait() end end function Check2() for i=1, #numtble do if i > 2 then if numtble[i] > numtble[i] - i then dum = numtble[i] - 1 table.remove(numtble,dum) elseif numtble[i] < numtble[i] - i then table.remove(numtble,numtble[i]) wait() print("Awesome") wait() end end end end function Winner() Check() Check2() for i,v in pairs(game.Players:GetPlayers()) do if v.PlayerGui:findFirstChild("Points"):findFirstChild("Posy"):findFirstChild("Numbers").Value == numtble[1] then v.Character.Head:Clone().Parent = game.Workspace wait() end end end Winner()
I highly recommend picking more descriptive names for your functions. It makes it much simpler to understand and write code.
I believe the problem is in your Check2
function. There are several large problems with it. I will note them here, however, my real suggestion is to completely rewrite Check2
.
1) numtble[i] > numtble[i] - i
is algebraically equivalent to 0 > -i
(which will never be true). I don't really know what you meant to be doing here.
2) table.remove(numtble,numtble[i])
table.remove
does not take an element to remove, it takes an index, so to delete the i
th element, the second argument should be i
, not sometable[i]
.
First, we note that Check1
does not really provide anything new. The information it gives is already available in a list, :GetPlayers()
, it's just useful for us to be able to delete to have it in a copy.
Now, Check2
simply eliminates everything from the list that Check1
makes, ideally leaving only the largest element. In other words, Check2
just finds the largest element in a list.
We can redefine Check2
to work in a much simpler way:
function FindLargest(list) -- For reusability, safety, should be an -- argument, rather than a global variable. local largest = list[1]; for i = 2, #list do if largest < list[i] then largest = list[i]; end end return largest; -- I am choosing to return instead of -- modifying numtbl, because it is cleaner and safer. end
Our Winner
function now looks like this:
function Winner() Check() local best = FindLargest(numtbl); for i,v in pairs(game.Players:GetPlayers()) do if v.PlayerGui:findFirstChild("Points"):findFirstChild("Posy"):findFirstChild("Numbers").Value == best then v.Character.Head:Clone().Parent = game.Workspace wait() end end end
I highly recommend changing the name of Check
to GetPlayerScores
or something similar. I also recommend making it return
the list rather than just set it to a value. Then we would have the Winner
function begin like this:
local best = FindLargest(GetPlayerScores());