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)
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.
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)
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
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)