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
01 | local db = true |
02 | script.Parent.Touched:Connect( function (hit) |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
04 | if db = = true then |
05 | db = false |
06 | local coinparts = script.Parent.Parent.Coins:GetChildren() |
07 |
08 | for i, v in pairs (coinparts) do |
09 | v.Transparency = 1 |
10 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
11 | script.CollectedSound:Play() |
12 | v.Transparency = 1 |
13 | end |
14 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
15 | player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 |
Explorer: https://prnt.sc/w7yqsn (I forgot to put it in explorer but the only thing in the script is CollectedSound, which plays)
01 | local db = true |
02 | script.Parent.Touched:Connect( function (hit) |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
04 | if db = = true then |
05 | db = false |
06 | local coinparts = script.Parent.Parent.Coins:GetChildren() |
07 |
08 | for i, v in pairs (coinparts) do |
09 | v.Transparency = 1 |
10 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
11 | player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 |
12 | script.CollectedSound:Play() |
13 | v.Transparency = 1 |
14 | wait(math.random( 5 , 15 )) |
15 | db = true |
First lets bust down your code and see whats wrong with it.
01 | local db = true |
02 | script.Parent.Touched:Connect( function (hit) |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
04 | if db = = true then |
05 | db = false |
06 | local coinparts = script.Parent.Parent.Coins:GetChildren() |
07 |
08 | for i, v in pairs (coinparts) do -- Start loop |
09 | v.Transparency = 1 -- Change transparency |
10 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
11 | player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 |
12 | script.CollectedSound:Play() |
13 | v.Transparency = 1 |
14 | wait(math.random( 5 , 15 )) -- wait 5-15 seconds |
15 | db = true |
Now for a working bit of code
01 | local db = true |
02 | script.Parent.Touched:Connect( function (hit) |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
04 | if db = = true then |
05 | db = false |
06 | local coinparts = script.Parent.Parent.Coins:GetChildren() |
07 |
08 | for i, v in pairs (coinparts) do -- Start loop to turn parts invisible |
09 | v.Transparency = 1 -- Change transparency |
10 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
11 | player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 |
12 | script.CollectedSound:Play() |
13 | v.Transparency = 1 |
14 | end -- End of first loop |
15 | wait(math.random( 5 , 15 )) -- wait 5-15 seconds inbetween loops |
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:
1 | local coinparts = script.Parent.Parent.Coins:GetChildren() --This returns a table, by the way. |
2 |
3 | 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. |
4 | v.Transparency = 1 --I recommend using v:IsA("ClassName") as well, so that the script doesn't error out. |
5 | end |