# Check for part position

I want to make a check so if a part is in a specified position a code will run. I’ve tried quite a lot and haven’t got any luck. Anyone know how to check for a parts position?

3 Likes

Can you explain in more detail what you’re trying to do? There are several ways to do something like this that could be more or less simple depending on what exactly your needs are.

Eg. If you’re not looking for a highly specific location you could use the Touched event on a trigger part and check if the touching part is the part you want, you could check a Region3 in a loop for if the part is within that region, or you could check on a loop if the distance between the part and the desired position is below some threshold.

I would probably just run a position check every time the changed event occurs for the part. Compare it to an actual position if it’s specific, or use magnitude if it’s a general location.

I just want to find out any way that I can check for a parts location. Like for instance, if a part is at the position of 1, 2 and 3 (x, y, z) I want to check whether that part is at that specific location and then a function to run.

Do if part.Position = Vector3.new(1,2,3) then. Note that this only applies if it’s exactly at that position, you might have to round the parts position.

1 Like

I’m asking what the situation you need this for is, so we can determine if this is something you need to do at all. Needing to do this is kind of gross and obscure. You probably have a different problem that you think this is the solution to, that may have a better solution.

In any case, if this part is not programatically set to the specific position and you’re expecting it to reach this position on it’s own naturally, you’re going to need to check if `math.abs((part.Position - targetPosition).magnitude)` is less than some threshold number (eg. 0.5), since the part’s position may not be exactly the number you want. If you’re setting the part’s position explicitly via another script, you can just compare the two positions with the == operator.

You will need to check this condition on a loop. Changed will not work because it doesn’t fire for physics related property changes. If we knew more details about what you’re trying to do at a higher level, maybe we could tell you if there’s a better way.

5 Likes

Alright, so I am trying to create an elevator and I want it so when the elevator reaches the top floor (position here blah) and the elevator is called back to the bottom, I want to ensure that the elevator has reached the top before making it come down. Just a normal check for its position.

What you should do is whenever you make the elevator go up, have a wait and then move it back down again. Instead of making an if statement.

1. Set the Primary Part of the elevator model to the elevator’s base (the platform players stand on).
2. At the floors you want players to stop at, have a part the exact same size and rotation as the elevator’s Primary Part. You can then call these parts something like “Floor1”, “Floor2”, etc
3. When you want the elevator to move to a certain floor use TweenService to reach your desired floor

To tween the elevator, I would do something like the following:

``````-- << VARIABLES >>
local tweenService = game:GetService("TweenService")
local info = TweenInfo.new()
local elevator = workspace.ElevatorModel
local floor1 = workspace.Floor1
local floor2 = workspace.Floor2
local floor3 = workspace.Floor3

-- << SETUP >>
elevator.PrimaryPart = elevator.Base

-- << LOCAL FUNCTIONS >>
local function tweenModel(model, CF)
local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Value = model:GetPrimaryPartCFrame()

CFrameValue:GetPropertyChangedSignal("Value"):Connect(function()
model:SetPrimaryPartCFrame(CFrameValue.Value)
end)

local tween = tweenService:Create(CFrameValue, info, {Value = CF})
tween:Play()

tween.Completed:Connect(function()
CFrameValue:Destroy()
end)
end

-- << MAIN >>
tweenModel(elevator, floor1)
--tweenModel(elevator, floor2)
--tweenModel(elevator, floor3)
``````

Obviously there is more logic behind that (for example you may want to work out the distances between each floor to ensure a constant speed).

https://www.robloxdev.com/api-reference/class/TweenService
https://www.robloxdev.com/api-reference/enum/EasingStyle
https://devforum.roblox.com/t/tweening-models/60545

3 Likes

I may try utilising this when I can because I love the example! I just have to know how to check for a parts position as I may use it for other projects.

For that, you could check the part’s Y position against other floor levels.

For example, if you do Steps 1 and 2 I mentioned above:

``````-- << GOING UP >>
local destination = workspace.Floor2
repeat
--Move elevator up
wait()
until elevator.PrimaryPart.Position.Y >= destination.Position.Y

-- << GOING DOWN >>
local destination = workspace.Floor1
repeat
--Move elevator down
wait()
until elevator.PrimaryPart.Position.Y <= destination.Position.Y
``````

Alternatively, you could use FindPartsInRegion3 to check if both platforms are in the same area but that would be a lot less efficient for this application.

I would just not let the elevator move again until it’s available using a variable, eg. moving = false.
I’d set it to true when a button is pressed and wait until it reaches the top, then wait a few extra seconds for people to get on and off, and then set moving to false again. This way, the elevator button only works when moving is false.

1 Like

Just a warning that this tween method isn’t the best. Using this method will result in visible model cracking if the code is run enough times. I’ve made a post that explores this method of tweening and provides an alternate to it, free of such errors. See: Brief Introduction to Tweening Models (Obsolete)

In terms of an elevator itself, you could probably make one through any combination of the following:

• TweenService
• BodyMovers
• Constraints

The code provided is a good way to “appetise” OP into a suitable solution for the issue.

1 Like