Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I keep getting this bug that every value is tied?

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

✓ Accepted answer

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()

3 comments

0

Shows same bug, but this time with 'Colour Rush' Instead of Obby - Moved votes to ReplicatedStorage instead of lighting.

0

I edited the script so that it supports ties now.

0

It keeps saying every value is tied even though one value changes to 1 before script is enabled.

This discussion is preserved as it was. New answers, comments, and votes are closed.