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

Why is it calling ImageTransparency a nil value?

Asked by 6 years ago

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

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.)

0
you actually index with integers in a array, not "meaningless numbers" User#23365 30 — 6y
0
They are not called "in pairs" loop. That is the wrong terminology. You are looking for the term "for". User#19524 175 — 6y
0
for loops include `for i=1,7 do`, `for i,v in ipairs(x) do` and `for i,v in pairs(x) do` User#22604 1 — 6y
0
"The other answer's solution will not work. The indexes in a :GetChildren() call are meaningless numbers, not the name of the object. This is (probably) because multiple children of an instance can have the same name." This is wrong. All arrays have indices, those are indices. Each item has its own index User#19524 175 — 6y
View all comments (9 more)
1
But they aren't called "in pairs" loops. They're for loops. Keyword for. User#19524 175 — 6y
0
yes, each item has its own index, and the point is that this isn't based on the name of the instance like the other person thought. meaningless as in unrelated to what the instance actually is User#22604 1 — 6y
0
alright, i'll edit my answer to be clear about the for loop User#22604 1 — 6y
1
That doesn't make any sense. Meaningless is the wrong term. User#19524 175 — 6y
0
Okay, this script didn't work out either. I had to switch a couple things around to fit my needs for the script, but all in all, it's changing the background transparency of the buttons, not the image transparency like I was hoping. If you could help fix this I'd greatly appreciate it. EnderGamer358 79 — 6y
0
^ Line 6 should say ImageTransparency instead of transparency, my bad. To incapaz, I guess I was unclear. I'll edit the answer to be clear about what I meant (unrelated to the value, not completely random) User#22604 1 — 6y
0
That still didn't do the trick. I'll fiddle around with the code a bit to see if that solves anything. EnderGamer358 79 — 6y
0
works for me, not sure what your issue is. did you fix line 2 where I put kept P instead of parent? (fixed in updated script) User#22604 1 — 6y
0
Ah, nevermind, it's working now. I had it set to search for ImageLabels instead of ImageButtons at Line 10. Thanks for your help! EnderGamer358 79 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

0
I think this is what you are doing, if something is off, like i got your hierarchy wrong or your imagelabel isnt name "ImageLabel" then change things around theking48989987 2147 — 6y
0
also you could've done local image = P.Parent:GetChildren()["ImageLabel"] but I did 2 lines of code to avoid confusion theking48989987 2147 — 6y
0
Sounds about right, except for one thing; It's calling "image" a nil value, because they aren't all called ImageLabel, I think. How would I fix this? EnderGamer358 79 — 6y
0
This isn't how GetChildren works at all, check the wiki https://www.robloxdev.com/api-reference/function/Instance/GetChildren User#22604 1 — 6y
View all comments (5 more)
2
this would return nil, as :GetChildren() returns a array not a dictionary User#23365 30 — 6y
0
^ User#19524 175 — 6y
0
oof theking48989987 2147 — 6y
0
ok I think the edit fixed it theking48989987 2147 — 6y
0
you might want to consider :FindFirstChildOfClass User#22604 1 — 6y

Answer this question