Coin Collection script needs help. I collect coins but parts don't change transparency. Only 1 part changes transparency
Edit: WizyTheNinja's answer worked but it gave me more than 1 coin, here is the script for only 1 coin
local db = true
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if db == true then
db = false
local coinparts = script.Parent.Parent.Coins:GetChildren()
for i, v in pairs(coinparts) do
v.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
script.CollectedSound:Play()
v.Transparency = 1
end
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1
wait(math.random(5,15))
for i,v in pairs(coinparts) do
db = true
v.Transparency = 0
end
end
end
end)
Explorer: https://prnt.sc/w7yqsn (I forgot to put it in explorer but the only thing in the script is CollectedSound, which plays)
local db = true
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if db == true then
db = false
local coinparts = script.Parent.Parent.Coins:GetChildren()
for i, v in pairs(coinparts) do
v.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1
script.CollectedSound:Play()
v.Transparency = 1
wait(math.random(5,15))
db = true
v.Transparency = 0
end
end
end
end)

4 comments
Are you sure the parts aren't turning invisible at all? From the looks of your code, you are waiting 5-15 seconds inbetween each part
I literally said only 1 part changes transparency on the second sentence. If you read what I wrote in the first sentence, you will be able that I am trying to make a coin collection part. It has debounce and I want it to turn all the parts visible after 5 to 15 seconds. The parts should become invisible when I touch them and after 5 to 15 seconds they become visible, debounce = true. @WizyTheNinja
Lua is not an asynchronous language. Your loop turns a part invisible, waits 5-15 seconds, turns the part visible again and then repeats for the next children. Correct me if I'm wrong, but I don't think this is the code behavior you intended. @Dave_Robertson
I understand exactly what you are trying to do, but I dont think you do. You are running a loop that changes ONE parts transparency, and then waits 5-15 seconds. The problem isn't that they arent going invisible, the problem is you arent doing every one, then waiting 5-15 seconds.