Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

0

Thank You. Your explanation was logical.

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!

2 comments

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

0

There are only Bool Values inside the Trails folder. And this did not help.

This discussion is preserved as it was. New answers, comments, and votes are closed.