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

How do I make a Bool Value true if all other 8 Bool Values are true? (Solved)

Asked by 4 years ago
Edited 4 years ago

I'm making a Mario 64 game and I'm trying to make an 8 Red Coins system and I'm designing it so there are 9 Bool values, 8 are called 'Collected1, Collected2, Collected3' etc. and the ninth is called 'CollectedAll'. I have already made it where if you touch a red coin it will teleport below the map and turns one of the Collected Bool Values 1-8 true but I want to know how to make it where if all of them are true, then 'CollectedAll' turns true. Thx for the help. NOTE: <--- person here AKA Me is noobish at scripting so I don't know how to go about making it detect if all the others are set to true.

local Coin1 = script.Parent.Collected1
local Coin2 = script.Parent.Collected2
local Coin3 = script.Parent.Collected3
local Coin4 = script.Parent.Collected4
local Coin5 = script.Parent.Collected5
local Coin6 = script.Parent.Collected6
local Coin7 = script.Parent.Collected7
local Coin8 = script.Parent.Collected8
local CollectedAll = script.Parent.CollectedAll

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Bool approach:

You could do it with bools by doing the following:

while wait() do
    local Coin1 = script.Parent.Collected1
    local Coin2 = script.Parent.Collected2
    local Coin3 = script.Parent.Collected3
    local Coin4 = script.Parent.Collected4
    local Coin5 = script.Parent.Collected5
    local Coin6 = script.Parent.Collected6
    local Coin7 = script.Parent.Collected7
    local Coin8 = script.Parent.Collected8

    if Coin1.Value == true and Coin2.Value == true and Coin3.Value == true and Coin4.Value == true and Coin5.Value == true and Coin6.Value == true and Coin7.Value == true and Coin8.Value == true then
        local CollectedAll = script.Parent.CollectedAll
        CollectedAll.Value = true
    else -- if there is one of the Coins that is not true
        local CollectedAll = script.Parent.CollectedAll
        CollectedAll.Value = false
    end
end

This works, but there's a better way of doing this / you'll run into some issues later on.

For example, what if you had 100 coins? 1000? You'd have to make 1000 variables (which you can't actually do. roblox limits you to having 60 variables at the top of your script, so after a certain point it becomes impossible to use bool for each coin.)

Also, what if we want to spawn a random amount of coins? We can't create bools in real time so we can't do it that way.


A More Flexible Approach:

Here's another way of doing it. This approach will let us use as many coins as we want.
We also don't have to create a variable for each coin. The script will see how many coins there are in the coin model and make that our totalNumCoins

My comments explain what's happening at each step.

I also provided a demo using the code below. You can check out the model here

numCoinsCollected = 0 --a variable to keep track of how many red coins we collected
redCoins = game.Workspace.Coins-- model containing all the red coins 
totalNumCoins = #redCoins:GetChildren() -- variable that keeps track of the total amount of coins

--go through all the coins in the coin group and create a touch event for each of them
for _, coin in pairs(redCoins:GetChildren())do
    --each coin has their own coinCollected true/false value
    --that keeps track of if the coin was collected or not
    local coinCollected = false


    --event that fires when the coin is touched
    coin.Touched:Connect(function(hit)
        --if the thing touching the coin is a player, have the player 'collect' the coin
        if(game.Players:FindFirstChild(hit.Parent.Name))then
            if(not coinCollected )then
                -- we destroy the coin since we collected it
                -- You can instead move the coin under the map like you said in your
                -- question if you'd like
                coin:Destroy()
                coinCollected = true

                --add one to the number of coins that we have collected
                numCoinsCollected = numCoinsCollected + 1

                --print out how many coins we have collected,
                --if we collected all the coins, tell the player that they have collected all the coins
                print("You have collected ".. numCoinsCollected .. " coins!")
                if(numCoinsCollected == totalNumCoins)then
                    print("All of the coins have been collected!")
                end
            end     
        end
    end)
end
0
thanks for the help! :D HistoricalCletus2005 2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You can use if statements:

while wait() do
    local Coin1 = script.Parent.Collected1
    local Coin2 = script.Parent.Collected2
    local Coin3 = script.Parent.Collected3
    local Coin4 = script.Parent.Collected4
    local Coin5 = script.Parent.Collected5
    local Coin6 = script.Parent.Collected6
    local Coin7 = script.Parent.Collected7
    local Coin8 = script.Parent.Collected8
    if Coin1.Value == true and Coin2.Value == true and Coin3.Value == true and Coin4.Value == true and Coin5.Value == true and Coin6.Value == true and Coin7.Value == true and Coin8.Value == true then
        local CollectedAll = script.Parent.CollectedAll
        CollectedAll.Value = true
    else -- if there is one of the Coins that is not true
        local CollectedAll = script.Parent.CollectedAll
        CollectedAll.Value = false
    end
end

I put the Coins variables in the while wait, because it will renew the variable each time.

Hope this helped!

0
thank you as well HistoricalCletus2005 2 — 4y

Answer this question