Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

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

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

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

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.

2 answers

✓ Accepted answer

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

2 comments

0

It changes the transparency but gives me more than 1 cash

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

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

1 comment

0

I did what you wrote but It only changes the transparency of 1 coin. Pls help

This discussion is preserved as it was. New answers, comments, and votes are closed.