How to make a limit for Mouse position while holding

  1. I Want to make the CuffPart move to the mouse position in a limited way, So it does not go very far away.

  2. It goes far away…

Remotes["Stepped."].OnServerEvent:Connect(function(Player, MousePosition: Vector3)
	CuffPart.Position = Vector3.new()
end)

You can try something like this, first of all you should calculate the distance between the CuffPart and the mouse position. If the distance exceeds a certain threshold, you can prevent the CuffPart from moving beyond that limit. Try this code and let me know if it worked.

local distancethreshold = 50 

Remotes["Stepped."].OnServerEvent:Connect(function(Player, MousePosition: Vector3)
    local currentPosition = CuffPart.Position
    local distance = (currentPosition - MousePosition).Magnitude
    
    if distance > distancethreshold  then
        local direction = (MousePosition - currentPosition).Unit
        
        MousePosition = currentPosition + direction * distancethreshold 
    end
    
    CuffPart.Position = MousePosition
end)

2 Likes

Well, It is working, But, There is not limit for the mouse, Same problem, I Am thankful for the help.

1 Like

Could you please be more specific about what you’re trying to do? That way, I could assist you more effectively.

1 Like

Well, I Am making a cuff, The cuffed person goes to the CuffPart, Which is why I am moving it, I Wish the player be able to move the CuffPart in the specific range of movement.

1 Like

Sorry for late answer, try this.

local maxDistance = 10 -- Define the maximum distance CuffPart can move from the player's position

Remotes["Stepped."].OnServerEvent:Connect(function(Player, MousePosition: Vector3)
    -- Get the player's position (assuming the player has a character and a primary part)

        local playerPosition = Player.Character.HumanoidRootPart.Position

        -- Calculate the direction and distance from the player to the mouse position
        local direction = (MousePosition - playerPosition)
        local distance = direction.Magnitude

        -- Clamp the distance to the maximum allowed distance
        if distance > maxDistance then
            direction = direction.Unit * maxDistance
        end

        -- Set the new position of the CuffPart
        CuffPart.Position = playerPosition + direction
    end
end)