Help with a weird error

Hello there! I just made my voting system but I don’t seem to find a way to fix this error that I get when i test it witch says: ’ Script:18: attempt to compare Instance and number ’
Here is my script:

local number = 0
local winner = nil

function GetWinner()
	local votes = {}
	
	for i, v in pairs(workspace.VotingPads:GetChildren()) do
		table.insert(votes, v)
	end
	
	for i, v in pairs(votes) do
		if v:FindFirstChild('Value').Value > number then
			number = v
			winner = v
		end	
	end
	for i, v in pairs(votes) do
		table.remove(votes, i)
	end
	return winner
end
GetWinner()

wait(1)

print(winner)

And also I don’t know what the names of the pads will be that’s why I didn’t stored them on a table and ‘Value’ is an intvalue stored on each pad.

Thank you!

What type of Game are you trying to make?

2 Likes
number = v
winner = v

Is that a instance value? If so that is the error. Just put

number = v.Value
winner = v.Value
2 Likes

The error is actually on the if statement line but I think what you said is going to help me with what’s after the if statement.

1 Like

The middle one, you forgot to tell the script which place to place the value. You should do:
table.insert(votes,#votes,v)

by the way you should declare the number value inside in function. Or the number value will never sets to zero then assuming the last round’s highest votes as a winner in the current round. Or you can write number = 0 in the first line of the function.

After I see what you described, I think it’s because that there v couldn’t find the thing called “Value” so it will return nil. So you need to add an if v:FindFirstChild('Value') ~= nil do before it.

2 Likes

I just found a solution! What I did is:

local number = 0
local winner = nil

function GetWinner()
	local votes = {}
	
	for i, v in pairs(workspace.VotingPads:GetChildren()) do
		table.insert(votes, #votes, v)
	end
	
	for i, v in pairs(votes) do
		if v:FindFirstChild('Value') ~= nil then
			local val = v:FindFirstChild('Value')
			if val.Value > number then
				number = v
				winner = v
			end	
		end
	end
	for i, v in pairs(votes) do
		table.remove(votes, i)
	end
	return winner
end

I just had to make a variable for the value and it worked!

Thanks everyone!

1 Like