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

[SOLVED] How do I use GetChildren() and change children's properties?

Asked by 4 years ago
Edited 4 years ago

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

01local db = true
02script.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
View all 23 lines...

Explorer: https://prnt.sc/w7yqsn (I forgot to put it in explorer but the only thing in the script is CollectedSound, which plays)

01local db = true
02script.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
View all 21 lines...
0
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 WizyTheNinja 834 — 4y
0
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 Dave_Robertson 42 — 4y
0
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 SimplyConst 0 — 4y
0
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. WizyTheNinja 834 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

First lets bust down your code and see whats wrong with it.

01local db = true
02script.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
View all 28 lines...

Now for a working bit of code

01local db = true
02script.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
View all 26 lines...
0
It changes the transparency but gives me more than 1 cash Dave_Robertson 42 — 4y
0
Nvm, fixed it by putting local player = game.Players:GetPlayerFromCharacter(hit.Parent), player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 at the end of the loop Dave_Robertson 42 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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:

1local coinparts = script.Parent.Parent.Coins:GetChildren() --This returns a table, by the way.
2 
3for 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.
5end
0
I did what you wrote but It only changes the transparency of 1 coin. Pls help Dave_Robertson 42 — 4y

Answer this question