Vector3 Part Position

so i have a part that when you click a button, its position moves + 17.84 studs on the Z axis. After a certain position on the Z axis, i want it to stop moving that direction. Here is the code i have currently.

local los = game.Workspace.LOS
local lmarker = game.Workspace.LOSmarker
local fd = game.Workspace.FD
local fmarker = game.Workspace.FDmarker
local button = script.Parent

button.MouseButton1Down:Connect(function()
      los.Position = los.Position + Vector3.new(0,0,17.84)
      lmarker.Position = lmarker.Position + Vector3.new(0,0,17.84)
end)

So as i said, heres what i want to achieve. After a certain position on the Z axis, i want it to stop moving that direction. Thanks.

1 Like

you can check if adding the scalar will achieve a number which is higher. If it does end the function, else run the code you already got.

here’s what i added, can you describe it more in depth please?

local los = game.Workspace.LOS
local lmarker = game.Workspace.LOSmarker
local fd = game.Workspace.FD
local fmarker = game.Workspace.FDmarker
local button = script.Parent

button.MouseButton1Down:Connect(function()
     los.Position = los.Position + Vector3.new(0,0,17.84)
     lmarker.Position = lmarker.Position + Vector3.new(0,0,17.84)

     if los.Position >= Vector3.new(150.43, 1.079, 164.496)then
   end
end)

A scalar is a vector component which describes amount. If we are checking the scalar for the Zed we would get it from its “Z” property. Remeber, the position is a vector data type.

Okay, I will try my best, although I am not exactly that advanced in scripting

los.Position.Z >= z component then

k, so this is what i added.

button.MouseButton1Down:Connect(function()
	los.Position = los.Position + Vector3.new(0,0,17.84)
	lmarker.Position = lmarker.Position + Vector3.new(0,0,17.84)
	
	if los.Position.Z >= 165.166 then
	end
end)

though, is that the correct way to completely stop the function?

If you arent listening for any other signals in that event you can just disconnect it

I mean, I’m not really looking for any other signals. How should I disconnect it?

You can create a local variable in its header scope to then be assigned to the event listener. From there it is simply calling the Disconnect() method to the variable we assigned to our event listener.

What event listener are you mentioning? Sorry if I don’t know many things, I’m not too advanced in scripting.

A event listener is basically :Connect() it describes when to fire the annonymous function

Yeah, in functions the Connect() returns a connection object which you can use to disconnect the function.

local connection
connection = button.MouseButton1Down:Connect(function()
    los.Position = los.Position + Vector3.new(0,0,17.84)
    lmarker.Position = lmarker.Position + Vector3.new(0,0,17.84)
    if los.Position.Z >= 165.166 then
        connection:Disconnect()
    end
end)