My code requires two clicks... why?

My code is requiring that players click on the cube once before they click and drag to position the cube. The desired behavior is to simply click-and-drag.

UserInputService.InputBegan:Connect(function(key, isSystemReserved)
    local part = mouse.Target
    mouse.TargetFilter = part
  if part then
      if part.Name == "cube" then
        guide.Transparency = 0.7
        while UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) do
          local mousePosition = mouse.Hit.p
          if mousePosition.X > constraintLeft.X and mousePosition.X < constraintRight.X then
            part.Position =, part.Position.Y, part.Position.Z)
            guide.Position =, guide.Position.Y, guide.Position.Z)
        guide.Transparency = 1

The test version of the game with the issue is located here.

1 Like

Why did you set the TargetFilter to the part when you click it?

1 Like

Also one problem I noticed, since you aren’t specifying what input type is used in InputBegan, the cube will be launched if you tap a key twice. Specify the input’s type with Enum.UserInputType, like this.

if key.UserInputType == Enum.UserInputType.MouseButton1 then

This was the source of the error. I have a placement part that the main part is aligned to so that no matter where the board spawns in the world the starting cube is in the right place. During my tests I filtered the cube instead of the placement part. It now works as I originally intended! Thank you.

Also, per the comment below, I had deleted another line while testing. Here is my user input conditional:

if key.UserInputType == Enum.UserInputType.MouseButton1 or key.UserInputType == Enum.UserInputType.Touch then
1 Like

Alright, glad to have helped, also I really got hooked onto your little game. I got like 20k points.

1 Like