Getting the distance between two parts without looping

Hello, so I’m currently working on a Press E to open door script to practice my scripting, and I was wondering if there was any way to get the players distance from the door without constantly using a while loop. I have tried searching it up and most topics I saw say to use Region3 but I was wondering if there’s a way to use Magnitude.

  • Any answers/Help are very appreciated. :relaxed:
local distance = (door.Position - character.Position).Magnitude

This will return the distance in studs from the door to the character

I already know how to do that, I’m trying to get the distance from the player without looping but thx for your reply. :slight_smile:

If this only happens while pressing E, then just do the check when they press. This removes the need for a while loop.

Example pseudocode:

UserInputService.InputBegan:Connect(function(input)
    if they pressed e and are near the part then
        the code
    end
end)

I’m trying to use the ContextActionService not UserInputService. I’m trying to make sure the character is less than 10 studs away from the door then I’m binding the E key to the Opendoor function.

Then just do the check within the bound function and just change it to work with context action service? I don’t see why you want to use a loop?

2 Likes

OMG I’m soooo dumb. Thx for making me realize this :slight_smile:. This is what happens when I don’t get enough ZZZ.

1 Like