Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would you implement a interaction mechanic?

Asked by 6 years ago

A common and practical trend in top ROBLOX games nowadays is to implement a press INPUT to interact mechanic, where input can be a key on a standard keyboard or a button on a console controller; allowing for both PC and Console players to more easily interact with game objects.

A great example of this mechanic would be the E to enter on Jailbreak cars.

Having this feature in mind, I'd like to pose a few open-ended questions regarding the internal mechanics of it, and request input on the most efficient way to tackle a successful implementation of an interaction system.

1) How do you find all the possible objects that can be interacted with?

I'd like to note that, while the question above is easily fixed by the recently-added CollectionService, I've decided to include it for the sake of the argument.

2) How do you find which object is closest to the player?

Have in mind there could be a large number of interactive objects.

3) How do you fire this interaction, considering there is a wide range of distinct objects with a wide range of functions.

2 answers

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago

Answer 1:-

How do we find anything? we just need to know something about it such as using a def name, type of instance or probably the best option would be to group them in a folder. Recursion may help but roblox have added in more functionality such as GetDescendants which may be useful.

Answer 2:-

A simple solution would be to use Magnitude but this would also work for things through walls which may not be required. Sp one possible solution would be to check using Raycasting from the humanoid to the instance to check if the "path" to this instance is not obstructed.

You should also think about how many checks need to be done ie only check the things that need to be done, if the player is not moving then no need to check.

Answer 3:-

There are a lot of possible ways to do this but they are mainly dependent upon the type of thing that you are interacting with.

One way would be to use a module script to hold any functions for local effects ect then I would probably send the instance in a remote event which should then be validated again on the server and any actions to be done on the server.

Summary

There is no real fixed solution and it all depends upon the complexity of the actions needed. Using module scripts can help by sharing the same functionality across the same instance type. Lastly any actions should be checked again on the server where it is safe.

0
Thanks for the answer! This resembles my currently implementation of this mechanic, though I am looking forward to see if other users find a more effective way that will not cause as much client lag when there's a large amount of objects. devSparkle 84 — 6y
Ad
Log in to vote
1
Answered by
Validark 1580 Snack Break Moderation Voter
6 years ago

While I typically recommend my Keys module for dealing with this kind of input, this would be one of the few scenarios in which I would seriously consider using ContextActionService.

You have a valid question, that is, "What is the best way to manage contextual actions?"

The best solution is usually going to come in the form of seeing whether Roblox has any built-in C or C++ functions you can leverage to accomplish your task, as Roblox doesn't use LuaJIT so "native" functions are typically going to be faster than Lua solutions if your scripts spend tremendous amounts of resources in a specific function, such as a recursive raycast distance checker.

Here is my very not humble opinion.

Stop with Rays. Are you shooting at interactive objects or trying to find distance? If you are shooting, you might even be able to use Mouse.Hit, which is the automatic raycast Roblox performs anyway.

If you aren't, then maybe magnitude would be much more efficient, but then you run into the issue of interacting through walls.

Well maybe there is an even more efficient way. A way of detecting proximity to end all ways of detecting proximity. And it is.... Invisible parts.

Think about it. It comes with two events: Touched and TouchEnded. Perfect for creating a boundaries system with perfect detection of when a player enters and leaves the area in which they should be prompted with an interaction button or a GUI signaling, "Now Leaving Kawaii Waifu Pillow Store". Perfect detection systems are indeed possible!

What? Am I hearing you aren't a big believer in the integrity of Roblox Part event-firing? Thou heretic! Well, I guess that's why we have GetTouchingParts.

I personally recommend a conservative balance between using both the events and the function aforementioned. You'd probably want to trust the detection of large zones to events, and possibly use the function when you aren't entirely convinced the events will be reliable.

I actually haven't written anything like this, and the last time I looked at a module for this was a while ago. I'd probably experiment initially with trusting the events, and then where it doesn't work I would make trade-offs.

A trade-off you might-make, for example, is you might try detecting whether they are in the room with a ton of interaction objects using a large transparent part that fills the room, and use the function to check which one should prompt them.

Good luck! I typed this up on my phone in the late hours of the school-night, but I hope something I said inspired you to create a better system.

Answer this question