Log in to vote

How to check if multiple parts have the same value?

Asked by
cancle5 105
8 days ago

Basically, I have 5 parts and in each part there's a value. Who can i check if all the values are the same.

I didnt want to do a for i, v in pairs since i wanted to do it all at once. Yes all the parts are the same

1 answer

Log in to vote
Answered by
sydre 214 Moderation Voter
8 days ago
if part1.valueName.Value == part2.valueName.Value and part2.valueName.Value == part3.valueName.Value and part3.valueName.Value == part4.valueName.Value and part4.valueName.Value == part5.valueName.Value then
    -- Your code here

Not sure why you wouldn't want to use an in pairs loop though, considering that it would make the code easier to read as well as making it more adaptable:

local parts = {part1, part2, part3, part4, part5}
local previousValue = nil
local sameValue = true

for _, part in pairs(parts) do
    if previousValue and part.valueName.Value ~= previousValue then
        sameValue = False
    previousValue = part.valueName.Value

Answer this question