Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how do i check if all statment are true?

Asked by 5 years ago
Edited 5 years ago

the condition triggers whenever one slot set true, ignoring the other 4 ones. How do i make it read all the slots and not the first one?

local full = workspace.model.Full -- a boolValue that will trigger when all values are true
local rep = workspace.model.Area -- it's a model with 5 parts, each part have a boolValue.

for _,v in pairs(rep:GetChildren()) do
    if v.Slot.Value == true then
    full.Value = true
    break                   
    end 
end

if full.Value == true then
--stuff
end

i know that i could do like:

if slot1 == true and slot2 == true and slot3 == true and slot4 == true and slot5 == true  then
--stuff
end

but the number of parts will not be the same.And it's a dirty way.. Thank you!

2 answers

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago
Edited 5 years ago

Well you can run a check on all the bool values and add it to a variable, and after that run a check with the amount of parts and the amount of "true" values

local full = workspace.model.Full -- a boolValue that will trigger when all values are true
local rep = workspace.model.Area -- it's a model with 5 parts, each part have a boolValue.
local NumberOfTrue = 0;
for _,v in pairs(rep:GetChildren()) do
    if v.Slot.Value == true then
    NumberOfTrue = NumberOfTrue +1                
    end 
end

if NumberOfTrue == #(rep:GetChildren) then
--stuff
end

Or since you only want the condition to be true when all the bool values are true you can also set the Full.Value to true first, and check all the bool values, once one of the bool is false, full.Value will be false

local full = workspace.model.Full -- a boolValue that will trigger when all values are true
local rep = workspace.model.Area -- it's a model with 5 parts, each part have a boolValue.
full.Value = true
for _,v in pairs(rep:GetChildren()) do
    if v.Slot.Value == false then
    full.Value = false
    break             
    end 
end

if full.Value == true then
--stuff
end
0
Thank you, you explanation helped me a lot! thank you again ! iagometroid 61 — 5y
0
No problem :) glad it helped aazkao 787 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

Well, an easy way to do this would a function that iterates through the table that returns true if all values of something are true and returns nil if any one of them are false:

local function CheckAll (tbl)
    for _,v in tbl do
        if not v.Value then
            return 
        end
    end
    return true
end

A simple breakdown of the previous function is:

As it iterates through the given table, if the value of any thing in the table is false or nil (a falsy value), it will return nil, which in itself is a falsy value. However, if none of them are false or nil, it will return true, which is a truthy value.

This is because the return function basically prevents all code underneath it inside its current scope from running.


Application


with that said, you could do the following:

local full = workspace.model.Full
local rep = workspace.model.Area

if CheckAll(rep:GetChildren()) then
    full = true
end

Answer this question