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:
P = script.Parent P.MouseEnter:connect(function() P.Size = P.Size + UDim2.new(0.06, 0, 0.03, 0) P.Position = P.Position - UDim2.new(0.03, 0, 0.015, 0) end) P.MouseLeave:connect(function() P.Size = P.Size - UDim2.new(0.06, 0, 0.03, 0) P.Position = P.Position + UDim2.new(0.03, 0, 0.015, 0) end) P.MouseButton1Click:connect(function() local B = P.Parent:GetChildren() for i=1, 20 do wait(0.04) B.ImageTransparency = B.ImageTransparency + 0.05 end B.Visible = false 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)
local parent = script.Parent -- renamed since P is unclear local parentsChildrenList = parent.Parent:GetChildren() -- instead of defining it with each click, define it ahead of time local linearTweenSpecs = TweenInfo.new(0.8, Enum.EasingStyle.Linear) -- take 0.8 seconds to fade out and have change of transparency consistent local tweenService = game:GetService("TweenService") -- to help with tweening so you don't need loops like you were using local transparency1Tween = {ImageTransparency = 1} -- so that we don't redefine it each tween parent.MouseButton1Click:Connect(function() for _,image in pairs(parentsChildrenList) do if image.ClassName == "ImageLabel" then -- if it's an imagelabel and not a textbox or something local transparencyTween = tweenService:Create(image, linearTweenSpecs, transparency1Tween) -- create a tween using the variables we defined earlier transparencyTween:Play() -- play the tween spawn(function() -- in a new thread (so all images can tween at same time) transparencyTween.Completed:Wait() -- wait for the tween to complete image.Visible = false -- make Visible false (like it was earlier) end) end end 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 :
P = script.Parent P.MouseEnter:connect(function() P.Size = P.Size + UDim2.new(0.06, 0, 0.03, 0) P.Position = P.Position - UDim2.new(0.03, 0, 0.015, 0) end) P.MouseLeave:connect(function() P.Size = P.Size - UDim2.new(0.06, 0, 0.03, 0) P.Position = P.Position + UDim2.new(0.03, 0, 0.015, 0) end) local function cycle (table,thing) for i,v in pairs(table) do if v == thing then return true end end return false end P.MouseButton1Click:connect(function() local image P.Parent:FindFirstChild("ImageLabel") or P.Parent:FindFirstChild("ImageButton")--maybe use find first descendant if image then for i=1, 20 do wait(0.04) image.ImageTransparency = image.ImageTransparency + 0.05 end image.Visible = false end end)
so this checks the table for "ImageLabel" and sets its visibility and transparency