With my current script, clicking on an object will cause it to move to the mouse’s position, but that mostly leads to it flying way off into the distance. How do I make it so the part only hovers a couple studs from the player, and not flying into the void?
local script:
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local SelectedPart
Mouse.Button1Up:Connect(function()
SelectedPart = nil -- Unselect part when the player releases their mouse
end)
Mouse.Button1Down:Connect(function()
local target = Mouse.Target
if not target then return end -- Check if there is a target
if not target:IsDescendantOf(workspace.GrabbableParts) then return end -- Check if target is a descendant of the folder
SelectedPart = target
end)
Mouse.Move:Connect(function()
local MousePositionIn3DSpace = Mouse.Hit.Position -- A vector3 containing the coordinates of the mouse's position in 3D space (aka Workspace)
if not SelectedPart then return end -- Check if the player is currently grabbing a part
SelectedPart.Position = Vector3.new(MousePositionIn3DSpace.X, .5, MousePositionIn3DSpace.Z)
end)