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

How to change a GUIs visibility when a value changes?

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)

1 comment

0

no answers?

1 answer

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.

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