Display Particle When Player is Close to Object

I want to make a button prompt that uses a particle effect to prompt the user to press a key. Something similar to Jailbreak’s key card mechanic, it prompts the player to hold E in order to obtain a key card from a police officer.

I was thinking about running a script inside the object that will loop until it finds a player and then display the particle effect locally, but this doesn’t seem like the most efficient way to do this. What’s the most efficient way to prompt a player when near an object?

This is the best and only way to do it as of now.

Magnitude may help?

You underestimate the computational expense of this. Checking the distance between characters is completely fine. So long as you’re not running any costly functions in your loop, this is completely fine.

What I’d personally suggest is containing your distance checks in a coroutine and setting a value when the player is near another player, then outside of that coroutine you can have your functionality related to interaction with nearby players.

Hello!

References
Camera Class: Camera | Documentation - Roblox Creator Hub

GetPartsObscuringTarget function: Camera | Documentation - Roblox Creator Hub

ScreenPointToRay function: Camera | Documentation - Roblox Creator Hub

ViewportPointToRay function: Camera | Documentation - Roblox Creator Hub

Suggestion
I’d recommend running the script locally so there’s less strain on the server and only allow the pickpocket to succeed if it catches it on the server with proper conditions (ie prisoner was less than X distance from officer).

Keep a loop that detects based on a mix of magnitude and camera, utilizing aforementioned Camera methods. I recommend this so that the user experience is more seamless.

Please let me know how it goes.

Thank you!

I don’t think all that overhead is necessary. DistanceFromCharacter is sufficient enough to determine how far away the player is from a certain point. If the character is a certain distance away, you can cast a ray towards that player to check if there’s any obscurity. If not, prompt the UI and enable functionality for pickpocketing. Some of it will be required on the server or perhaps mirror functionality so that the client isn’t holding authority for this behaviour.

None of the methods related to camera are relevant here because those don’t help you to determine a physical distance or point between the character and another character. You’re checking for distance between objects, not between the camera and another point.

I also think you didn’t mean to reply to me. :wink:

1 Like

I was reading a topic you made a while ago about interactive objects and Region3. How did you end up implementing Region3? I think that’s the best solution to my problem. Btw, I think I mislead the topic because the object will be stationary not necessarily like Jailbreak’s key card mechanic. :frowning:

I ended up using the method I posted above and not Region3. My map wasn’t large enough for me to determine that the usage of Region3s would be more appropriate over simply checking distance.

My method was a Region3 created at half second intervals using the player’s position, that is used to make pre-set particle parts enable/disable, different color blocks are different particles. It also cycles through foliage and sets nearby ones to visible. With the entire map’s foliage set to transparent before it loads in, the script won’t have to worry about setting the entire model to transparent at the beginning.