Making a script only run if frame is visible

Good evening dev forum peeps, i have a pretty simplish question that im trying to figure out and the gist is that i have a screen gui with a frame in it and inside that frame is an image label with a pretty simple local script which changes the image back and fourth

while true do
	script.Parent.Image = "rbxassetid://5816448737"
	wait(1)
	script.Parent.Image = "rbxassetid://5196722532"
	wait(5)
	
end

The problem here is that the screen gui/frame is not always visible on the screen and i only want the script to run if that said screen gui is actually visible (this is cause it still seems to cycle the images even if the screen gui/frame isnt visible). How would i end up doing this?

Thanks in advanced

You can make use of the :GetPropertyChangedSignal and checking visiblity like so. Depending on your intended behavior place a second check and wait if you want it to reserve the last seen image or revert to one or the other.

local visible_signal = frame:GetPropertyChangedSignal("Visible")
while true do
	if not frame.Visible do
		visible_signal:Wait()
	end

	script.Parent.Image = "rbxassetid://5816448737"
	wait(1)
	script.Parent.Image = "rbxassetid://5196722532"
	wait(5)
end
2 Likes

would i just need to define what “frame” is beforehand?

Yes whatever your frame is you want to check visiblity of; one of these mentioned frames

i have a screen gui with a frame in it and inside that frame is an image label

1 Like

Okay so i presume something like this?

local frame = script.Parent.Parent.Parent
local visible_signal = frame:GetPropertyChangedSignal("Visible")
while true do
	if not frame.Visible do
			visible_signal:Wait()
		end

		script.Parent.Image = "rbxassetid://5816448737"
		wait(1)
		script.Parent.Image = "rbxassetid://5196722532"
		wait(5)
end
1 Like

You forgot “then do” but thats cool. thanks dude

2 Likes

ah I get my luaisms confused apologies. I think it’s actually just if not frame.Visible then the extra do is not necissary; it creates an extra scope to close with end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.