You can write your topic however you want, but you need to answer these questions:
I am trying to figure out how to make a shop that involves you to be near it and press E to activate the UI that corresponds with each different shop.
My problem is that I worry that magnitude is too harmful to performance in order to determine how close they are to a shop or NPC. Even when using heartbeat.
I considered Region3 but with some shops being rotated and region 3 not working with local rotation it seems obvious I am out of ideas.
Magnitude is not very harmful to performance as it isn’t an operation. Magnitude is a property of Vector3, so the actual performance hit you would take it the subtraction of the two Vector3s, which isn’t significant. Especially if you do this client-side (which you should), I would not worry about performance at all.
You will absolutely not notice any significant drop in performance doing a magnitude check every heartbeat. If anything, Region3s would be worse for performance in this case since you’d end up making tons of tables and need to iterate through them.
If you’re still really concerned about performance, I just checked OP again and you don’t even need to check the magnitude each heartbeat. Instead, you can only check it when the player presses E.
I was gonna originally suggest to check it each time they press E but if they want a UI Icon to pop-up when they get within a range, then they’d have to check every so often regardless. The better suggestion is doing “zones” as looking at the OP picture, seems they will have different area and etc… it’s literally pointless to loop and check magnitude on something that they aren’t even near and shouldn’t be able to interact with.
OP, just periodically check if they are within your zones, or even set it as they enter each one and then load and loop-check the things they should be able to interract with.
Yes, I like and agree with this solution. If there are multiple locations for stores, I personally would create a folder with the parts I use for checking magnitude, then have a function loop through them and return true if the player is close to one.
I personally will define a bounding box around the shop. I then listen on boundingBox.Touched to make sure all parts are picked up accurately, and in the callback for this and TouchEnded as well as a slow loop I use GetTouchingParts and find character’s root parts. I can then show/hide the menu for all players instantly (however maybe not accurately 100% of the time) and automatically correct for error. Any players who just entered the bounding box should open the GUI (the client can perform the check to know when to enable the GUI and the server can verify this for shop requests. Any players who leave it should close the menu.
This actually manages to be slightly faster than Region3 with small parts since it’s a similar check on less parts and supports rotation.
There’s a way around this using .Running on Humanoid so you don’t have to check on each heartbeat. This only works when the player has stopped walking though.
Player.Character.Humanoid.Running:connect(function(speed)
if (speed == 0) then
--Check magnitude
end
end)
I would use CollectionService instead of putting the parts in a folder. Tagging the individual stores and iterating through that collection means you don’t need to compromise your workspace hierarchy, which may save a headache later on.
Check out this click E to sit tutorial. You can easily change this to work with your shop. It uses collection service, which in my opinion, is the best way of doing it. https://www.youtube.com/watch?v=japnJf9P34U