Doing something once values equal true

Basically i have 12 different BoolValues, and i need something to happen once all of them are true, how would i do that?

For now my idea was to make something along these lines:

while wait() do
	game.Players.PlayerAdded:Connect(function(player)
		local value = player.values.Value1
	end)
end

Get a variable for every value and make an if statement that if all of them are true then do the code.

But don’t know if that would work and i think there can be an easier way to do it, so could anyone help me please?

Thanks! :slightly_smiling_face:

2 Likes

Having race conditions like these are very troublesome and I wouldn’t recomend doing this but, you could tag the bold values with CollectionService and asynchronously loop through them, this is a sloppy implementation:

local totalValues = 12
local counter = 0

for _, bool in pairs(CollectionService:GetTagged("BoolValues")) do
    if bool.Value then
        counter += 1
    end
end

if counter == totalValues then 
    doSomething()
end
2 Likes

Thanks, but could you please explain what i need to do or change in that script in order for it to work, i have never used CollectionService before and i’m a bit confused in general.

Hm, I believe what you could do is get the children of all the values inside your values Folder (I’m assuming), and use a Changed Event to check if all of them are equal to true? Not sure on how to do it though, which is the thing

game.Players.PlayerAdded:Connect(function(Player)
    local Connection

    local values = player:WaitForChild("values")
    local ActivatedBools = 0
    local TotalBools = #values:GetChildren()

    for _, Bool in pairs(values:GetChildren()) do
        local function Changed(NewValue)
            if NewValue == false then
                ActivatedBools -= 1
            else
                ActivatedBools += 1
            end
            print("Activated Bools: "..ActivatedBools)
            if ActivatedBools == TotalValues then
                Connection:Disconnect()
                break
            end
        end
        Connection = Bool.Changed:Connect(Changed)
    end
    print(Player.Name.." has enough BoolValues!")
end)

Or just something relevant to what @ObedientDavid_B suggested

3 Likes

Oh, I’d suggest going with @JackscarIitt 's implementation then!

1 Like

I think the break line is not needed, because there is no loop going on.

Should i add one or do something?

I think it is actually needed, in order to break through the loop of getting all the values since you’re encasing it inside a Changed Event

Again I’m not sure if this would entirely work since it is a sample code, but you can try it I suppose (Wait I forgot the Disconnect function exists)

1 Like

Should i set Totalvalues to 12?

The # will pretty much get the length of a table, or the amount of objects there are

If I were to put this:

local Table = {"Hello", 32, true}
print(#Table)

I’d get an Output of 3, as there are 3 things inside the table! (Aka “Hello”, 32, and true)

Yes, that’s TotalBools, but i am asking about TotalValues in:

if ActivatedBools == TotalValues then

Wait I didn’t even define TotalValues oops

Just change that to TotalBools and you should be all set

I tested it and changed all the values to true, i don’t think it worked.

When i clicked play it instantly said i had all the values, though all of them being false.

Hm, alright then…
Could you try this?

game.Players.PlayerAdded:Connect(function(Player)
    local Connection

    local values = player:WaitForChild("values")
    local ActivatedBools = 0
    local TotalBools = #values:GetChildren()

    for _, Bool in pairs(values:GetChildren()) do
        local function Changed(NewValue)
            if NewValue == false then
                ActivatedBools -= 1
            else
                ActivatedBools += 1
            end
            print("Activated Bools: "..ActivatedBools)
            if ActivatedBools == TotalValues then
                Connection:Disconnect()
            end
        end
        Connection = Bool.Changed:Connect(Changed)
    end
    
    while ActivatedBools ~= TotalBools do
        print(Player.Name.." does not have enough BoolValues!")
        wait(1)
    end
    print(Player.Name.." has enough BoolValues!")
end)

Now it says that i don’t have enough boolvalues, but when i do set them all to true it stills says that i don’t

Are you changing them on the client or on the server-side? That might be why if that’s the case

If not, then it’s probably having something to do with my pairs loop

I am doing it server-sided. Should i do the opposite?

Nah, let me go ahead and test it out on Studio real quick to see what the issue could be :thinking:

Can you double check to make sure that all the BoolValues are parented to the values variables? It seemed to work fine for me no problem

1 Like

I’ll send you yhe script i’m using with some modifications:

local service = game:GetService("BadgeService")
local badgeid = 2124707878


game.Players.PlayerAdded:Connect(function(Player)
	local Connection

	local values = Player:WaitForChild("eggHuntLB")
	local ActivatedBools = 0
	local TotalBools = #values:GetChildren()

	for _, Bool in pairs(values:GetChildren()) do
		local function Changed(NewValue)
			if NewValue == false then
				ActivatedBools -= 1
			else
				ActivatedBools += 1
			end
			print("Activated Bools: "..ActivatedBools)
			if ActivatedBools == TotalBools then
				Connection:Disconnect()
			end
		end
		Connection = Bool.Changed:Connect(Changed)
	end

	while ActivatedBools ~= TotalBools do
		print(Player.Name.." does not have enough BoolValues!")
		wait(1)
	end
	print(Player.Name.." has enough BoolValues!")
	service:AwardBadge(Player.UserId, badgeid)
end)

eggHuntLB is a folder inside the player

The print("Activated Bools: "..ActivatedBools) statement does work, right?