✓ Accepted answer
#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
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
redCoins = game.Workspace.Coins
totalNumCoins = #redCoins:GetChildren()
for _, coin in pairs(redCoins:GetChildren())do
local coinCollected = false
coin.Touched:Connect(function(hit)
if(game.Players:FindFirstChild(hit.Parent.Name))then
if(not coinCollected )then
coin:Destroy()
coinCollected = true
numCoinsCollected = numCoinsCollected + 1
print("You have collected ".. numCoinsCollected .. " coins!")
if(numCoinsCollected == totalNumCoins)then
print("All of the coins have been collected!")
end
end
end
end)
end
1 comment
thanks for the help! :D