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)
First lets bust down your code and see whats wrong with it.
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 -- Start loop v.Transparency = 1 -- Change transparency 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)) -- wait 5-15 seconds db = true v.Transparency = 0 end -- end of loop. The layout of this is your problem. Lua will not run through all of the parts and perform each line for all children then move on. It will run all the lines for one child at a time. end end end) --This layout will do the following --1. Find child --2. Change child transparency to 1, aswell as reward cash --3. Wait 5-15 seconds --4. Turn child back visible --5. Find next child and repeat steps 2-5
Now for a working bit of code
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 -- Start loop to turn parts invisible v.Transparency = 1 -- Change transparency local player = game.Players:GetPlayerFromCharacter(hit.Parent) player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 script.CollectedSound:Play() v.Transparency = 1 end -- End of first loop wait(math.random(5,15)) -- wait 5-15 seconds inbetween loops for i,v in pairs(coinparts) do -- Start another loop to turn parts back visible db = true v.Transparency = 0 end -- End of second loop. This is how you would have to set it up if you want the turn all parts invisible, wait 5-15 seconds, then turn them back visible. end end end) --This layout will do the following --1.Iterate through all children turning each one invisibile and rewarding cash --2. waits 5-15 seconds --3. Iterates through all the children again, this time turning each one visible
You can use an in pairs
loop, which loops through a table. There's 2 values in the for loop, i and v. i means index, which is just a number, and not the value we need. the v means value, which is a type of value. It can be any value (boolean, number, string, instance, etc.) Let me give you an example:
local coinparts = script.Parent.Parent.Coins:GetChildren() --This returns a table, by the way. for i, v in pairs(coinsparts) do --This looks complicated. I've already explained i and v, but the coinparts is the actual table itself. Put it in these brackets. v.Transparency = 1 --I recommend using v:IsA("ClassName") as well, so that the script doesn't error out. end