When I manually change the value, it works but doesn't when changed in game??
Script is disabled at the start and enabled in another script when someone has pressed 'Vote' text button.
Changed to kikitobs answer - similar bug. It keeps saying every value is tied even though one value changes to 1 before script is enabled.
01 | local Voted |
02 | local Tied = { } |
03 |
04 | for i,v in pairs (game:GetService( "Lighting" ).Votes:GetChildren()) do |
05 | if Voted then |
06 | if v.Value > Voted.Votes then |
07 | Voted = { Votes = v.Value,Name = v.Name } |
08 | Tied = { } |
09 | elseif v.Value = = Voted.Votes then |
10 | table.insert(Tied,v.Name) |
11 | end |
12 | else |
13 | Voted = { Votes = v.Value,Name = v.Name } |
14 | end |
15 | end |
Your current way getting the highest vote is really inefficent and it is doing way to many unneeded checks also changing the variable Mapvotes to {} after you disable it is just useless.
You can just loop trough the Votes children and check if the Voted value is higher then the last one. You should also just make it a variable what is voted instead of checking everything to see what it is.
Heres the rewritten script:
01 | local Voted |
02 | local Tied = { } |
03 |
04 | for i,v in pairs (game:GetService( "Lighting" ).Votes:GetChildren()) do |
05 | if Voted then --If it doesn't exist then it will not check if the Votes is higher then it to prevent attempted to compare number with nil error |
06 | if v.Value > Voted.Votes then --Checks if the votes are higher then the current stored one |
07 | Voted = { Votes = v.Value,Name = v.Name } --Set it to the stored |
08 | Tied = { } |
09 | elseif v.Value = = Voted.Votes then |
10 | table.insert(Tied,v.Name) |
11 | end |
12 | else |
13 | Voted = { Votes = v.Value,Name = v.Name } --If it doesn't store a vote yet it will set it to the first one |
14 | end |
15 | end |