How to detect when a frame crosses another frame?

So, I have made a spin/crate system. The way it works is it sends a request to the server, the server sends back what item did the player get and then a sample of random items is generated using a table where it is assigned how many items of each rarity should be in the sample. Then this is all put into a frame and the frame is tweened until it reaches the last object which is the item the player got.

(WARNING: calm music)

External Media

The only thing that is left is creating a clicking sound every time a frame passes through the middle of the holder frame. How do i do that? Maybe there is an gui hitbox module you know or anything else?

You can just use math for that. Besides, simply detecting if Frames are touching won’t work well at high speeds because it can be moving too fast for it to be detected.

Here’s what I came up with:

  • Use modulo operator a % b to find the “progress” of passing through one item
  • If the progress resets, that’s how you know it has completely passed and you should play the sound effect

Note that this method only works in one direction, because the way you detect if the progress has reset is if it decreases.

Example code:

--assuming that the ScrollingFrame ONLY scrolls downwards

local previousProgess: number = 0
game:GetService('RunService').RenderStepped:Connect(function()
	local progress: number = sf.CanvasPosition.Y % frameSize
	if progress < previousProgess then --if the progress is LESS than before, it's because it has reset
		sound:Play()
	end
	previousProgess = progress
end)
1 Like

Took me like 20 mins to get it but now it works perfectly fine! Thank you :grin:

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