Moving model up/down

I have a script that moves the model up and down but I would like to stop letting the model move after it reaches a specific height position.

local Camera = workspace.CameraSystem.RailClose

function onClick(click)
		Camera:SetPrimaryPartCFrame(Camera:GetPrimaryPartCFrame() * CFrame.new(0,-1,0))
		wait()
	end
	
script.Parent.ClickDetector.MouseClick:connect(onClick)

This is what the script looks like.

1 Like

Just add an if statement:

local set_height = 20

-- in function
if Camera:GetPrimaryPartCFrame().y < set_height then
--
else
--

Did I place it correctly if not then that explains why it’s not working?

local Camera = workspace.CameraSystem.RailClose
local set_height = -20


function onClick(click)
		Camera:SetPrimaryPartCFrame(Camera:GetPrimaryPartCFrame() * CFrame.new(0,-1,0))
	wait()
	if Camera:GetPrimaryPartCFrame().y < set_height then

	else

	end
	
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

Run through the logic of the function. In order for the if statement to work it would have to be like this:

local Camera = workspace.CameraSystem.RailClose
local set_height = -20


function onClick(click)
	if Camera:GetPrimaryPartCFrame().y < set_height then
        Camera:SetPrimaryPartCFrame(Camera:GetPrimaryPartCFrame() * CFrame.new(0,-1,0))
	    wait()
	else
        print("Too high")
	end
	
end

The else is unnecessary unless you want to do something if the maximum height is reached. If you have a minimum value as well you could use similar Boolean logic: Camera:GetPrimaryPartCFrame().y > min_height.

With this script, the Camera doesn’t move at all whatever the height is.

To recap, I want to make a button that moves the camera down, but I want to limit the camera’s height. Say like -20

What’s the Camera’s starting height? If you want to move the camera down then just do what I suggested with min_height.

The PrimaryPart’s y cord is 0.625

I refer to my previous answer.

You could throw in a conditional statement like @PerilousPanther suggested, or you could clamp the Y value.

local Camera = workspace.CameraSystem.RailClose

function onClick()
	local cf = Camera:GetPrimaryPartCFrame()
	Camera:SetPrimaryPartCFrame(cf * CFrame.new(0, math.clamp(cf.Y - 1, -20, .625), 0))
end

script.Parent.ClickDetector.MouseClick:connect(onClick)
1 Like

Change the < in the conditional statement to a >.

I went with a simpler option where the camera will go straight to the needed hight
This is the script:

local Camera = workspace.CameraSystem.RailClose

function onClick()
	Camera.MainPart = Vector3.new(23.018, -5, -49.876)
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

But I got this error:

MainPart is not a valid member of Model “Workspace.CameraSystem.RailClose” - Server - Script:4

My solution isn’t really complicated at all, the clamp function of the math library takes a value, and forces it to stay within a certain range.

Besides, it’s telling you that there isn’t a part called MainPart inside of your model. You’re also doing this:

Camera.MainPart = Vector3.new(23.018, -5, -49.876)

You need to add .Position to the end of MainPart.

Camera.MainPart.Position = Vector3.new(23.018, -5, -49.876)
Camera.MainPart.Position = Vector3.new(23.018, -5, -49.876)

is moving only the MainPart, not the hole model

I would suggest reading up on lua and Roblox API syntax. I don’t see why you wouldn’t want to do this:

1 Like

That’s why you need to use the SetPrimaryPartCFrame function of the model.

local model = workspace.CameraSystem.RailClose
local cFrame = model:GetPivot()
local counter = 0
local moveSpeed = 1
local moveAmount = 10

script.Parent.ClickDetector.MouseClick:connect(function(player)
    counter = (counter + moveSpeed) % (math.pi * 2)
    model:PivotTo(cFrame + Vector3.new(0, math.sin(counter) * moveAmount, 0))
end)

or

local model = workspace.CameraSystem.RailClose
local counter = 0
local move = 1

script.Parent.ClickDetector.MouseClick:connect(function(player)
    counter += 1
    model:PivotTo(model:GetPivot() + Vector3.new(0, move, 0))
    if counter < 10 then return end
    counter = 0
    move *= -1
end)
1 Like

This is perfect I would like to know what numbers I need to change to make it revert?