Best Place To Place Code To Close a Shop Based on Distance

Hey All,

In my game a player can click on a store monitor to bring up a store menu for purchasing items. The store menu is a GUI with Frame and Buttons. Clicking the Buttons will take you to additional store areas. Pretty straight forward. I have about a dozen different store area Frames that a player might make Visible.

What I need to create is a simple way to close any of the Frames that might be visible if the player moves too far away from the store monitor.

Within a LocalScript, I was thinking of creating a table to hold all the shop Frames. Then setup a function/event to detect the distance between the player and the shop monitor. I’ve seen use of RunService and Magnitude to do that. If the distance exceed a specified value, then loop through the shop Frame table and make all the Frames Visible = false.

Where should I add this code? Should this code be placed in a script by itself or merged with the main shop LocalScript? I’m afraid that if I add it to the main shop LocalScript, monitoring for distance might interfere with the rest of the script from running properly.

Any other suggestions or considerations to this setup?

Use a part and use the Touched event.

Note that if you set frame.visible to false, all children will not be visible as well. No need to loop through a table unless you are using the table for a different reason. Also a suggestion for checking the distance: you can connect the humanoid.running event to make it check the distance when the player moves, that means that you don’t have to have a while loop running all of the time

Bad idea considering how unreliable Touched events are and especially TouchEnded.

I highly recommend Region3 since you get more accurate results in detecting the region of where the player is in.

I never mentioned using TouchEnded. Using boolean and Touched works.

And even if you do want to use Touched and rely on its unhinged operations, it’s bad practice and will only reinforce the idea that you shouldn’t be solely relying on Touched for different procedures. Same goes towards bool values.

Make it client-sided and make use of Humanoid event, and Magnitude. (Or Region3, Raycast, whatsoever)