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

How to change a GUIs visibility when a value changes?

Asked by 6 years ago
Edited 6 years ago

I am trying to make is so everytime a value changes a script changes the visibility of a image button. It won't work it does nothing. It is in a local script.

pID=script.Parent.prgrmID.Value
icnID=script.Parent.prgrmID.icnID.Value
if(pID==0 and icnID==0)then
    script.Parent.Visible=false
elseif(pID>0 and icnID>0)then
    script.Parent.Visible=true
elseif(pID>0 and icnID==0)then
print("ICON NOT FOUND")
script.Parent.prgrmIcon.Image="http://www.roblox.com/asset/?id=413746335"
end


script.Parent.prgrmID.Changed:connect(function()
if(pID==0 and icnID==0)then
    script.Parent.Visible=false
elseif(pID>0 and icnID>0)then
    script.Parent.Visible=true
elseif(pID>0 and icnID==0)then
print("ICON NOT FOUND")
script.Parent.prgrmIcon.Image="http://www.roblox.com/asset/?id=413746335"
end
    end)

0
no answers? User#19341 0 — 6y

1 answer

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

Both pID and icnID will hold the value. This value is never changed in the code so when the changed event is fired nothing will be changed as the variable have not been updated to the current values.

-- these variables are set once
local pID = script.Parent.prgrmID.Value -- this is the value no the instance
local icnID = script.Parent.prgrmID.icnID.Value  -- also use local variables

if(pID==0 and icnID==0)then
    script.Parent.Visible=false
elseif(pID>0 and icnID>0)then
    script.Parent.Visible=true
elseif(pID>0 and icnID==0)then
    print("ICON NOT FOUND")
    script.Parent.prgrmIcon.Image="http://www.roblox.com/asset/?id=413746335"
end

The changed event passes the new value as an argument.

We also should take a look at the logic elseif(pID>0 and icnID==0)then will never run since icnID>0 on the if statement before so we should check this first.

-- variables
local scriptPar = script.Parent -- the scripts parent since we access it a lot
local icnID = scrptPar.prgrmID.icnID -- this is the instance not the actual value

scriptPar.prgrmID.Changed:Connect(function(newVal) -- use the new changed value

    -- you should check this logic
    if newVal == 0 and icnID.Value == 0 then -- get the current icnID.Value 
        scriptPar.Visible = false
    elseif newVal > 0 and icnID.Value == 0 then
        scriptPar.prgrmIcon.Image="http://www.roblox.com/asset/?id=413746335"
    elseif newVal > 0 and  icnID.Value > 0 then
        scriptPar.Visible = true
    end

end)

I hope this helps, please comment if you do not understand how / why this code works.

Ad

Answer this question