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

How to check if any of the children's value is set to True?

Asked by
Soban06 410 Moderation Voter
3 years ago

I have a folder inside the player called "Trails", the folder contains some bool values. Named 'OwnsRedTrail' and 'OwnsBlueTrail'.

What do I want?

I want to check that, if the player has any of those 2 bool values set to true, then I want to show the frame. If both of the values are set to false, then I don't want to show the frame.

What have I tried?

Well, I have tried the following approach:

local Player = game.Players.LocalPlayer

while true do
    for i, trail in pairs(Player.Trails:GetChildren()) do

        if trail.Value == true then
            script.Parent.Visible = true
        else
            script.Parent.Visible = false
        end

    end

    wait(2)
end

But the above script is checking if all the bool values are set to true or false. I want to check that if ANY of the values is set to true. No matter it is the ''OwnsRedTrail' or 'OwnsBlueTrail'..

Thanks for any help

2 answers

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

You can use return for that.

Let's for example make a function which will check if player has at least one of the trails.

local function HasTrail()
    for i, Trail in ipairs(Player.Trails:GetChildren()) do
        if Trail.Value == true then
            return true
        end
    end

    return false
end

What is happening? Well so first it loops through all the trails. Then it checks if Trail.Value == true, if it is then it will return true, which means that it will return the function, stop it, like completely stop and return the true value. By returning it, the function will no longer be checking for new values. But if none of the trails have returned true then the function will still be running and after pairs loop, it will return false because if nothing has returned yet then it means that none of the trails have true value.

Also if you are not going to use i variable, it's good to use ipairs instead of pairs since it is faster, what ipairs is that if it hits nil value it breaks the loop but in your case you know that the trails will always be there so it will never break it but only make your loop faster.

In your case you can use this function like this:

local Player = game.Players.LocalPlayer

while true do
    script.Parent.Visible = HasTrail() -- Has trail will return true or false, true if atleast one trail had true value, false if none of them had true value
    wait(2)
end

Sorry for my explanation, i know it's long and hard to understand but luckily this is simple code so i guess you can understand it without the actual explanation.

0
Thank You. Your explanation was logical. Soban06 410 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Try doing this code (descriptions are in code but meh moderators don't care so it means it will check if it's a bool value, but if it's not, will do if it's possibly a string value with true and false values):

local Player = game.Players.LocalPlayer

while true do
    for i, trail in pairs(Player.Trails:GetChildren()) do
    if trail:IsA("BoolValue") then -- trails should be BoolValues so it can determine if it is `true` or `false`
            if trail.Value == true then
                    script.Parent.Visible = true
            else
                    script.Parent.Visible = false
            end
    else -- if it might be a StringValue
        if trail.Value == "true" then
                    script.Parent.Visible = true
            elseif trail.Value == "false"
                    script.Parent.Visible = false
            end
        end
     end

    wait(2)
end

Hope this helps!

0
Include a description of the problem, instead of saying "Try doing this code"! Here's how you should format your answers: https://scriptinghelpers.org/help/how-post-good-questions-answers JesseSong 3916 — 3y
0
There are only Bool Values inside the Trails folder. And this did not help. Soban06 410 — 3y

Answer this question