Identifying if all values in a table are the same- am I doing this right?

I’m writing more badge scripts wooo.

I want to award players a badge when all of the BrickColors of the Children in a model I have are identical. While I’ve scripted things to check if 2 or more values in a table are the same, I’ve never tried with checking if all values are the same.

I figured if I had a sort of “master” brick that would dictate what color the other bricks should be that it would make it simpler to do this. There are 64 children in this model, so I’m pursuing tables as individually labelling and identifying these BrickColors would be inefficient and a hassle.

But I’m not really sure how to continue? Here’s the script so far:

local BadgeService = game:GetService("BadgeService")
local badgeid = 0
local bricks = game.Workspace.randomcolorbricks:GetChildren()
local master = game.Workspace.randomcolorbricks.RandomColorBrickMaster
local mastercolor = master.BrickColor

local samecolor = 0

for _, v in pairs(bricks) do
	for _, val in pairs(bricks) do
		if v.BrickColor == val.BrickColor then
			print( "Found a match! " .. v.BrickColor .. " is the same as " .. val.BrickColor)
			local samecolor = samecolor + 1
		end
	end
end

if samecolor == 64 then
	local players = game:GetService("Players"):GetPlayers()
	for i,v in pairs(players) do
		BadgeService:AwardBadge(v.UserId, badgeid)
	end
end

If you’re helping, please offer code examples with your explanations! Thanks!

1 Like

You could use a master and compare, or you could just loop through all the bricks, checking at every index that the brickcolor is equal to the last. If it’s not, you know not all the colors are equal. It’d look like this:

for i in range(2, #bricks + 1) do
    if bricks[i].BrickColor ~= bricks[i-1].BrickColor then
        -- Not equal, escape
        break
    else
        samecolor += 1
    end
end
local Game = game
local Players = Game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local BadgeId = 0 --Change this to the badge's ID.
local Bricks = nil --Define bricks here,
local MasterBrick = nil --Define the 'MasterBrick' here.

local State = true
for _, Brick in ipairs(Bricks) do
	if Brick.BrickColor ~= MasterBrick.BrickColor then State = false break end
end

if State then
	for _, Player in ipairs(Players:GetPlayers()) do
		local Success, Result = pcall(function() return BadgeService:AwardBadge(Player.UserId, BadgeId) end)
		if not Success then warn(Result) continue end
	end
end

This is returning BrickColor is not a valid member of Script at line 8.
For reference, my line 8 is:

if Brick.BrickColor ~= MasterBrick.BrickColor then State = false break end
local Bricks = nil --Define bricks here,
local MasterBrick = nil --Define the 'MasterBrick' here.

That’s an issue with how you’ve defined these two variables.

Odd, because these should work just fine.

local Bricks = game.Workspace.randomcolorbricks:GetChildren()
local MasterBrick = game.Workspace.randomcolorbricks.RandomColorBrickMaster

Both of these should return Parts (or a table of them, in Bricks’s case).

Full script for further reference:

local BadgeService = game:GetService("BadgeService")
local badgeid = 0
local Bricks = game.Workspace.randomcolorbricks:GetChildren()
local MasterBrick = game.Workspace.randomcolorbricks.RandomColorBrickMaster

local State = true
for _, Brick in ipairs(Bricks) do
	if Brick.BrickColor ~= MasterBrick.BrickColor then State = false break end
end

if State then
	print("badge awarded")
	for _, Player in ipairs(Players:GetPlayers()) do
		BadgeService:AwardBadge(Player.UserId, BadgeId)
	end
end

local Bricks = game.Workspace.randomcolorbricks:GetChildren()
This must contain children which aren’t ‘BasePart’ instances.

You can either add if Brick:IsA("BasePart") then to the loop or remove any non-‘BasePart’ children.

If you want to find matches, try this.
I’m sorry I don’t like the case you wrote it in so I changed a lot of it

This is basically the same as @Forummer’s reply, but his is going to be better if you ever increase the count.

local BadgeService = game:GetService("BadgeService")
local BadgeID = 0
local Bricks = game.Workspace.randomcolorbricks:GetChildren()
-- // Filter out the sneaky little Script (and anything else)
for Index,Brick in ipairs(Bricks) do if not Brick:IsA("BasePart") then table.remove(Bricks, Index) end end
local MasterBrick = game.Workspace.randomcolorbricks.RandomColorBrickMaster
local MasterColor = MasterBrick.BrickColor

local Matches = 0
for _, Brick in pairs(bricks) do
    -- // I really like the new ternary syntax. It just reads really nice.
	Matches += if Brick.BrickColor == MasterColor then 1 else 0
end

-- // You can add as many bricks as you would like, and not need to worry about changing the value here.
if Matches == table.getn(Bricks) then
	local Players = game:GetService("Players"):GetPlayers()
	for Index, Player in pairs(Players) do
		BadgeService:AwardBadge(Player.UserId, BadgeID)
	end
end

Works like a charm. Thanks a ton!