'if part.Position == Vector3.new(...) then' isn't working

What I’m trying to do is to check if a part is past the position shown in the script. But it won’t work. Help

if script.Parent.Position <= Vector3.new(2585.25, 1567, -4246) then
	script.Parent.Default1.Transparency = 1
	script.Parent.Default2.Transparency = 1
	script.Parent.Robot1.Transparency = 0
	script.Parent.Robot2.Transparency = 0
	script.Parent.badgeScript.Enabled = true
	wait(4)
	script.Parent.Default1.Transparency = 0
	script.Parent.Default2.Transparency = 0
	script.Parent.Robot1.Transparency = 1
	script.Parent.Robot2.Transparency = 1
	script.Parent.badgeScript.Enabled = false
end

I know this is probably on the DevForum but I tried searching for it for a bit and I couldn’t find much. I’m also too tired to figure it ouyt so i neeeeed heeellppppp !!!111!
no errors btw

3 Likes

I think you would have to check the distance between the two Vector3s.

local Distance = (Pos1.Position - Pos2.Position).Magnitude -- Negative should mean it is behind, positive should be forward (i think)
1 Like

You can add “Magnitude” at the end of the vector3 values:

if script.Parent.Position.Magnitude <= Vector3.new(2585.25, 1567, -4246).Magnitude then
	script.Parent.Default1.Transparency = 1
	script.Parent.Default2.Transparency = 1
	script.Parent.Robot1.Transparency = 0
	script.Parent.Robot2.Transparency = 0
	script.Parent.badgeScript.Enabled = true
	wait(4)
	script.Parent.Default1.Transparency = 0
	script.Parent.Default2.Transparency = 0
	script.Parent.Robot1.Transparency = 1
	script.Parent.Robot2.Transparency = 1
	script.Parent.badgeScript.Enabled = false
end
2 Likes

How would I check if the part is between the magnitude/distance?

1 Like

please make a variable for script.Parent

2 Likes

Does the part travel on all axes or only one (X, Y, Z)? If it travels on a specific axis, you can check the position on that axis directly:

local endPosition = Vector3.new(2585.25, 1567, -4246)
if script.Parent.Position.X < endPosition.X then
   ...
end

You could apply the same principle to check the Y and Z axis using and if the position changes on all axes.

1 Like

use less than or equal to operator

1 Like

I changed it up a bit, but this doesn’t work as well. I’m not sure what to do.

local part = script.Parent
local startPosition = Vector3.new(2585.25, 1567, -4246)
local endPosition = Vector3.new(2566.25, 1567, -4246)

if part.Position.X > endPosition.X and part.Position.X < startPosition.X then
	print("working")
	part.Default1.Transparency = 1
	part.Default2.Transparency = 1
	part.Robot1.Transparency = 0
	part.Robot2.Transparency = 0
	part.badgeScript.Enabled = true
	wait(4)
	part.Default1.Transparency = 0
	part.Default2.Transparency = 0
	part.Robot1.Transparency = 1
	part.Robot2.Transparency = 1
	part.badgeScript.Enabled = false
end

The thing is, if I just keep the part.Position.X > endPosition.X it works, but if I change it to part.Position.X > startPosition.X it doesn’t. Also the same with using or and and respectively.

1 Like

Ok so building up from what @buutterfIIes said, you need to use magnitudes to compare distances. Have a reference part or point anywhere in the workspace (I will use a BasePart today)

local ReferencePartPos = workspace.ReferencePart.Position -- My part is at (10,10,10), put yours anywhere
if (script.Parent.Position - ReferencePartPos).Magnitude <= (Vector3.new(2585.25, 1567, -4246) - ReferencePartPos).Magnitude then
	script.Parent.Default1.Transparency = 1
	script.Parent.Default2.Transparency = 1
	script.Parent.Robot1.Transparency = 0
	script.Parent.Robot2.Transparency = 0
	script.Parent.badgeScript.Enabled = true
	wait(4)
	script.Parent.Default1.Transparency = 0
	script.Parent.Default2.Transparency = 0
	script.Parent.Robot1.Transparency = 1
	script.Parent.Robot2.Transparency = 1
	script.Parent.badgeScript.Enabled = false
end

Directly comparing the positions dont work as they’re Vector3 values and not numbers (which is what magnitude returns)

1 Like

Both should work …

if position.X <= targetPosition.X and
   position.Y <= targetPosition.Y and 
   position.Z <= targetPosition.Z then --something like that, have to test

--other way

local position = script.Parent.Position
local targetPosition = Vector3.new(2585.25, 1567, -4246)
local distance = (position - targetPosition).Magnitude

if distance <= 0 then

You cannot compare vectors, and magnitude will only give you the absolute distance from the world position of 0, 0, 0 (meaning it will always give you a positive number).

Vectors are nothing but numbers, and unless given a specific direction, your position comparison will always be relative to the world axis. Ensure you are doing the right comparison operator since you are also working with negative numbers working in a bounding box environment.

I would recommend using Unit and Dot product to check if a part is in front/past another part which allows for specified axes/rotation. Here’s an example model you can drag into the Explorer:
DotProductExamples.rbxm (4.3 KB)

Or you can use a built-in query API such as GetPartBoundsInBox to check if your part is within a certain bounds, similar to what you are trying to achieve with your start position and end position.

1 Like

@MysteryX2076 @2112Jay Both don’t work even when I change them a little.
@OniiSamaUwU I am very confused on what to do with the model you gave me lol. I don’t know how to script THAT well or understand that script haha

…also, is it important to say now that the part that is going to be changed is in a tween animation or no…? sorry

In your 1st post the <= Vector3 is what went wrong. Cannot directly compare Vector3 value using a operator. But you can with a component of it. I did all 3 XYZ … you may only need 1 for your case.
if position.X <= targetPosition.X then

This is what I wrote using your script.

local part = script.Parent
local position = part.Position
local targetPosition = game.Workspace.RobotPos1.Position
local distance = (position - targetPosition).Magnitude
print(distance)

if distance <= 21 then
	part.Default1.Transparency = 1
	part.Default2.Transparency = 1
	part.Robot1.Transparency = 0
	part.Robot2.Transparency = 0
	part.badgeScript.Enabled = true
	wait(4)
	part.Default1.Transparency = 0
	part.Default2.Transparency = 0
	part.Robot1.Transparency = 1
	part.Robot2.Transparency = 1
	part.badgeScript.Enabled = false
end

The distance printed is 21 but I don’t know what else to do with that. I want the decals to be visible and the badgeScript to be enabled AFTER a certain X position, just to be clear.

I was just working with it … this would be a point the part is close to and it would be the same after it passed. You could create a area like you did 21 or larger. Could make this work if it was on the edge of where you need it to check. Really don’t know what you’re doing other than tweening a part.
Maybe it would be better to check how far it is away from where it started.

Yeah I kinda took a different approach since I was a bit tired from working on this one thing.
For anyone wondering, I just made a BoolValue and then made a .Changed script.

By the way, what should I do with this whole post because my solution is irrelevant to the first post?

1 Like