Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I keep getting this bug that every value is tied?

Asked by 5 years ago
Edited 5 years ago

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.


local Voted local Tied = {} for i,v in pairs(game:GetService("Lighting").Votes:GetChildren()) do if Voted then if v.Value > Voted.Votes then Voted = {Votes = v.Value,Name=v.Name} Tied = {} elseif v.Value == Voted.Votes then table.insert(Tied,v.Name) end else Voted = {Votes = v.Value,Name=v.Name} end end if #Tied > 0 then script.Parent.Text = table.concat(Tied,", ").." are tying with "..Voted.Votes.. "votes." else script.Parent.Text = Voted.Name.." won with "..Voted.Votes.." votes." end script.Parent.Parent:Destroy()

1 answer

Log in to vote
1
Answered by
Kikitob 105
5 years ago
Edited 5 years ago

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:

local Voted
local Tied = {}

for i,v in pairs(game:GetService("Lighting").Votes:GetChildren()) do
    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
        if v.Value > Voted.Votes then --Checks if the votes are higher then the current stored one
            Voted = {Votes = v.Value,Name=v.Name} --Set it to the stored
            Tied = {}
        elseif v.Value == Voted.Votes then
            table.insert(Tied,v.Name)
        end
    else
        Voted = {Votes = v.Value,Name=v.Name} --If it doesn't store a vote yet it will set it to the first one
    end
end

if #Tied > 0 then
    script.Parent.Text = table.concat(Tied,", ").." are tying with "..Voted.Votes.. "votes."
else
    script.Parent.Text = Voted.Name.." won with "..Voted.Votes.." votes."
end

script.Parent.Parent:Destroy()
0
Shows same bug, but this time with 'Colour Rush' Instead of Obby - Moved votes to ReplicatedStorage instead of lighting. willkiller13 43 — 5y
0
I edited the script so that it supports ties now. Kikitob 105 — 5y
0
It keeps saying every value is tied even though one value changes to 1 before script is enabled. willkiller13 43 — 5y
Ad

Answer this question