Attempting to find a NumberValue's parent

I am making a voting system, and for the calculation script of which color won, i want the color that got the most votes win. And the winner color becomes white.

And well the issue is that i have a table of all the votes of each part, and the votes is a NumberValue called “votes”. So for the script to find the value, it needs to use,

votes.Value

And then i want of course the winning color above, to become white. But the i can’t get the value’s parent because the value is a number and therefore can’t have a parent. The error i keep getting says this “Workspace.Vote.Script:8: attempt to index number with ‘Parent’”.

I have tried a long time watching youtube videos, and looking through the devforum. But none of them have given me quite what i’m looking for.

If red have won, it would look something like this:

wait(10)
local votes = {script.Parent.VotePads.red.MainScript.votes.Value, script.Parent.VotePads.green.MainScript.votes.Value, script.Parent.VotePads.blue.MainScript.votes.Value}
local highest = math.max(unpack(votes))

print(highest)

for _,v in pairs(script.Parent.VoteResult:GetChildren()) do
	if v.Name == highest.Parent.Parent.Parent.Name then
		v.Color = Color3.fromRGB(255, 255, 255)
	end
end

Any help would be appreciated.

Try this

wait(10)
local votes = {script.Parent.VotePads.red.MainScript.votes.Value, script.Parent.VotePads.green.MainScript.votes.Value, script.Parent.VotePads.blue.MainScript.votes.Value}
local highest = math.max(unpack(votes))

print(highest)

for _,v in pairs(script.Parent.VoteResult:GetChildren()) do
	if v.Name == highest.Parent.Parent.Parent.Name then
		v.Color = Color3.fromRGB(255, 255, 255)
	end
end

Don’t use the .value in the table and use it in the for loop.

Where in the for loop do i put it?

When u get the max value. .

Since you are storing an integer rather than an instance within the table, you will be unable to call upon the parent of the value. This is why you are receiving the “Workspace.Vote.Script:8: attempt to index number with ‘Parent’” error. If you are unfamiliar with the concept of Data Types in programming, I would recommend having a read about them here, otherwise, if you are unfamiliar with what Data Types Roblox offers, I would recommend taking a look at their documentation.

With that being said, I would also advise you to change the hierarchy of the objects in such a manner that it makes it easier for you to reference each relevant instance. It will help you remove the unnecessary clutter and the need to constantly refer to the parent of each instance. It also makes it a lot more convenient when you are using loops to traverse tables of data.

Here is an example how I would organise the objects within the game in order to use them for the voting system…

image

With this hierarchy, I can easily reference every instance and value without having to use .parent since the hierarchy is organised. This organisation also allows me to be more flexible with the code, and even introduce a loop that can work dynamically regardless of how many more VotingPads you introduce into the system.

local highest = nil
for i, v in pairs(VotingSystem:GetChildren()) do
    print(v) -- prints the instance
    print(v.VotePad.Value) -- prints the value stored within VotePad
    if not highest or (highest and highest.VotePad.Value.Value <= v.VotePad.Value.Value) then
        highest = v
    end
end
print(highest) -- prints the instance that has the highest value stored in the Votepad

Hopefully, this provides you some insight in regards to keeping your hierarchy nicely organised and allows you to program a little bit more with ease.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.