I'm trying to use GetChildren to take several frames and tell them all to slowly fade out and set their visibility to false, and I'm doing this with ImageTransparency. However, when I try to do this with the script, it gives me an error code. How would I fix this?
Script:
01 | P = script.Parent |
02 |
03 | P.MouseEnter:connect( function () |
04 | P.Size = P.Size + UDim 2. new( 0.06 , 0 , 0.03 , 0 ) |
05 | P.Position = P.Position - UDim 2. new( 0.03 , 0 , 0.015 , 0 ) |
06 | end ) |
07 |
08 | P.MouseLeave:connect( function () |
09 | P.Size = P.Size - UDim 2. new( 0.06 , 0 , 0.03 , 0 ) |
10 | P.Position = P.Position + UDim 2. new( 0.03 , 0 , 0.015 , 0 ) |
11 | end ) |
12 |
13 | P.MouseButton 1 Click:connect( function () |
14 | local B = P.Parent:GetChildren() |
15 | for i = 1 , 20 do |
16 | wait( 0.04 ) |
17 | B.ImageTransparency = B.ImageTransparency + 0.05 |
18 | end |
19 | B.Visible = false |
20 | end ) |
Error Code: 15:05:39.262 - Players.EnderGamer358.PlayerGui.MenuGui.Base2.PlayButton.ButtonHandler:17: attempt to perform arithmetic on field 'ImageTransparency' (a nil value) 15:05:39.264 - Stack Begin 15:05:39.265 - Script 'Players.EnderGamer358.PlayerGui.MenuGui.Base2.PlayButton.ButtonHandler', Line 17 15:05:39.265 - Stack End
Your script doesn't work because B
is a table. If you want to do this for every item in the table, you can loop through with a for loop
using in pairs
(see below). Another issue with your script is that you're manually going through the ImageTransparency in a loop when you could simply tween from one value to another.
(Comments of script should have a fine explanation but if you want more information on Tweens or GetChildren
then check the wiki)
01 | local parent = script.Parent -- renamed since P is unclear |
02 | local parentsChildrenList = parent.Parent:GetChildren() -- instead of defining it with each click, define it ahead of time |
03 |
04 | local linearTweenSpecs = TweenInfo.new( 0.8 , Enum.EasingStyle.Linear) -- take 0.8 seconds to fade out and have change of transparency consistent |
05 | local tweenService = game:GetService( "TweenService" ) -- to help with tweening so you don't need loops like you were using |
06 | local transparency 1 Tween = { ImageTransparency = 1 } -- so that we don't redefine it each tween |
07 |
08 | parent.MouseButton 1 Click:Connect( function () |
09 | for _,image in pairs (parentsChildrenList) do |
10 | if image.ClassName = = "ImageLabel" then -- if it's an imagelabel and not a textbox or something |
11 | local transparencyTween = tweenService:Create(image, linearTweenSpecs, transparency 1 Tween) -- create a tween using the variables we defined earlier |
12 | transparencyTween:Play() -- play the tween |
13 | spawn( function () -- in a new thread (so all images can tween at same time) |
14 | transparencyTween.Completed:Wait() -- wait for the tween to complete |
15 | image.Visible = false -- make Visible false (like it was earlier) |
16 | end ) |
17 | end |
18 | end |
19 | end ) |
The other answer's solution will not work. The indexes in the table returned by a :GetChildren()
call are numbers that aren't related to the instance since it is a table, not a dictionary.
Edit: Added wiki links
Edit 2: I was wrong about the "meaningless numbers" part, updated answer to be more clear on what I originally meant. (Not related to the instance rather than completely random and meaningless.)
the reason is that B is a table, not an image label/button. To change that be can do this :
01 | P = script.Parent |
02 |
03 | P.MouseEnter:connect( function () |
04 | P.Size = P.Size + UDim 2. new( 0.06 , 0 , 0.03 , 0 ) |
05 | P.Position = P.Position - UDim 2. new( 0.03 , 0 , 0.015 , 0 ) |
06 | end ) |
07 |
08 | P.MouseLeave:connect( function () |
09 | P.Size = P.Size - UDim 2. new( 0.06 , 0 , 0.03 , 0 ) |
10 | P.Position = P.Position + UDim 2. new( 0.03 , 0 , 0.015 , 0 ) |
11 | end ) |
12 |
13 | local function cycle (table,thing) |
14 | for i,v in pairs (table) do |
15 | if v = = thing then return true end |
so this checks the table for "ImageLabel" and sets its visibility and transparency