How to detect when something is visible in a Scrolling Frame

I’ve been experimenting with methods of detecting when a viewport frame is visible inside a ScrollingFrame. So far this is what I got. It really doesn’t work most of the time. If you have any tips on how to do this please let me know.

local StartPos = script.Parent.CanvasPosition.X
local EndPos = (script.Parent.CanvasPosition.X + script.Parent.AbsoluteWindowSize.X)
		
for _,viewport in pairs(script.Parent:GetChildren()) do
	if viewport:IsA("ViewportFrame") then
		local startviewpos = viewport.AbsolutePosition.X
		local endviewpos = viewport.AbsolutePosition.X + viewport.AbsoluteSize.X
		if (startviewpos >= StartPos) or (endviewpos <= EndPos) or (startviewpos <= EndPos) or (endviewpos >= StartPos) then
			viewport.LoadViewport.Value = true
		else
			viewport.LoadViewport.Value = false
		end
	end
end
1 Like

Take a look at this article by EgoMoose. It covers how to detect when two 2D objects intersect. In your case, you’re finding if the ViewportFrame intersects with the ScrollingFrame. https://developer.roblox.com/articles/2D-Collision-Detection

2 Likes

i think u should put line4 at line 3 and line 3 at line 4

Thank you! It works the value changes now. Now All I have to do is set up the viewport frame.

1 Like

It has been a while on this post, I was wondering if you can explain how and what you are doing to check this, Do you use while loops every frame or detect scroll change to make it do the loop?