I would like to make a script that would move a part’s X position between two boundaries relative to the X position of the mouse on the viewport ray.
Here is a picture to illustrate what I’m trying to achieve:
This is what I have so far - it tweens a part to the left or right, depending on the viewport position of the mouse.
Note: Position 1 and Position 2 are the boundaries
local Mouse = Player:GetMouse()
local Position1 = game.Workspace.Position1.Position
local Position2 = game.Workspace.Position2.Position
local Part = game.Workspace.Part
local db = true
local TweenService = game:GetService("TweenService")
local Tween2 = TweenService:Create(Part, TweenInfo.new((Part.Position - Position2).Magnitude), {Position = Position2})
local Tween1 = TweenService:Create(Part, TweenInfo.new((Part.Position - Position1).Magnitude), {Position = Position1})
Mouse.Button1Down:Connect(function()
local Vector = Vector2.new(workspace.CurrentCamera.ViewportSize.X / 2, workspace.CurrentCamera.ViewportSize.Y / 20)
if Mouse.X < Vector.X and db then
if Part.Position ~= Position1 then
db = false
Tween1:Play()
Tween1.Completed:Wait()
db = true
end
elseif Mouse.X > Vector.X and db then
if Part.Position ~= Position2 then
db = false
Tween2:Play()
Tween2.Completed:Wait()
db = true
end
end
end)