I’m making a restaurant game of sorts, and I want to detect when a player is standing on/above a pad for a given station so their camera can move to a specified point and allow them to interact with certain objects in said station. On top of that, I also want to detect when they leave the pad itself. For a while I mulled over whether or not to use regions or the touched event, but I decided to go with touched because it’s faster and I thought I’d be able to deal with its jankiness, but it hasn’t been working too well; too many overlapping checks and balances that make the script hell to go through. Some issues:
Standing right at the edge of the part throws a lot of errors (but not consistently) because some of the scripts require casting a ray to detect whether or not the player is still standing on the object. It’s a complicated issue that I think might be more trouble than it’s worth to fix.
I’m basically just asking if this is the best way to do it or if there is a better solution to this. If so, please point me in that direction, thanks!
Just use a raycast every frame. Save ray.instance in a variable at the end of your code. The next frame compare the new ray instance with the last instance, and check if they are not equal. If so that means there is something new under the player. So just check if the new one is a platform or not, from there you can fire some sort of bindable or run a function, but that is your pseudo-event.
Yeah I’m at a weird crossroads of my scripting journey where I’ve been scripting for so long and have seen how bad optimization can kill games and I don’t really have the proper education on what is and isn’t exactly bad practice for performance. I don’t want spaghetti code and things to pile up over time, yknow
Nonetheless, I feel like BasePart.Touched should suffice here. I doubt you need to the accuracy to support troublesome players who wish to touch the pad with their pinkie toes. If you’re getting errors, it’s most likely an implementation issue
I mean, it was janky regardless. Sometimes it would double fire even with a proper debounce, and I just don’t want to deal with the occasional weirdness. This won’t be a totally core mechanic of the game, but I want the stuff players interact with a lot to have a >99.9% chance of working exactly as intended
By “double-fire”, I assume you mean the function you connected to BasePart.Touched was executing more than the debounce intended? This is not an reliability issue with the event, this is an implementation issue
It wasn’t double-firing because it would run twice for fun without a debounce. It’s likely due to the same reason it will kind of freak out when the player is just barely at the edge of the pad because it’s instantaneously adding the character to the debounce table, removing them, and then re-adding them immediately after. It’s less of an implementation issue than it is touched working as it should
It is working as it should. The physics engine is justifiably detecting when a part starts and stops contacting another part. It sounds like your code was not ensuring the entire assembly of the character was no longer contacting the part before removing them from the debounce table.
For reference, I created a minimalist Zone+ module based on spatial queries that would do this. You can take a look at the source code. You will not notice any erroneous firing of its artificial events: https://github.com/Ziffixture/Roblox/blob/main/PlayerTracker.lua