UserInputService Range

I’m decently new to scripting, however I know enough to make my own projects. To make some things, I need to understand more about UserInputService. The thing I need help with most right now, is how to make a range for UserInputService. For example, in Flee the Facility, when you walk near a door, it gives you the GUI and option to press E to open. How would I make UserInputService work for only the thing I’m in the correct range for? Thank you for reading.

2 Likes

You can look into magnitudes. When the user presses the button, it checks if the magnitude is smaller than a number you set. This will tell you if the range is close enough to the object you want.

3 Likes

Thanks, this is helpful, however I don’t think this was an answer to what I meant. I’m sorry for being confusing but what I meant was more like; “If a player is within 10 studs of the door a GUI label pops up telling me to press e to do -insert action here-”

I have the same problem about this as well, but I think my solution is just RunService.Heartbeat to constant check the magnitude between all player’s character model’s primary part current position and a NPC primary part. So it would be like:

local distance = (playerCharacter.PrimaryPart.Position - NPCModel.PrimaryPart.Position).Magnitude

Then RunService.Heartbeat to constantly check if distance <= maxRange, where maxRange is the maximum distance the player must be in to have like the GUI E turn on, and use a remote event to FireClient, then make the client to listen for this event being fired. When FireClient, just FireClient to whoever is near the NPC, then trigger the GUI.

1 Like