Best approach to making Press E shops

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.

Example of a shop

Any help would be greatly appreciated.

4 Likes

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.

6 Likes

my issue lies with updating it when a player gets closer or further from the target shop.

Searching for “press e” in #development-support:scripting-support returns a number of related threads. Here’s one: Making a door use keydown if player presses e.

Did you even try magnitude before saying this?

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.

2 Likes

yes but another issue is that the code gets rather messy. Furthermore my main question is if there is a better solution.

No it doesn’t. It’s literally one line of code:

if (HumanoidRootPart.Position - ShopPart.Position).magnitude <= minradius then

You need to try solutions even if you think it makes your code messy. You can always clean it ip later.

You honestly sound like you’re making excuses to not use magnitude for whatever reason.

And also no, in this case I think magnitude is the best solution.

2 Likes

Simply past experience. But I was less experienced so you are right.

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.

And I apologize if I came off as condescending.

1 Like

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.

3 Likes

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.

You have the idea I couldnt get across.

Main issue is Region3 cant accurately create said “zones”

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)
6 Likes

I’ll look at this. This is helpful.

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.

4 Likes

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

I forgot about CollectionService :laughing: Been out of scripting for years and only just come back to it, so not up to date just yet!

1 Like