I've been developing a script where if you press the right button down of the mouse, a clone will be brought into the Workspace; the position depends on what side the Part was clicked on, ex., if the mouse hovers on the top side of the part, the new position of the placement of the clone will be 3 studs higher in the y-direction, for a 3 by 3 by 3 block.
The problem is, that sometimes the function is activated more than once even though it is specified that it should only be cloned per button down. When tested both in Studio and online, the outcome is the same in-game: the clones would be created in sets of two or more, instead of one.
The following is my script:
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() xBuild= Mouse.Target.Position.X yBuild= Mouse.Target.Position.Y zBuild= Mouse.Target.Position.Z if tostring(Mouse.Target)== "DirtBlockClone" then if Mouse.TargetSurface== Enum.NormalId.Bottom then --print("bottom") if Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then workspace.numClonedBricks.Value= workspace.numClonedBricks.Value+1 local clone= workspace.DirtBlock:Clone() clone.Name= "DirtBlockClone" clone.Parent= workspace.DirtBlockCloneGroup clone.Position= Vector3.new(xBuild,yBuild-3,zBuild) --workspace.DirtBlockCloneGroup.DirtBlockClone:Destroy() end end if Mouse.TargetSurface== Enum.NormalId.Bottom then --print("bottom") if Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then workspace.numClonedBricks.Value= workspace.numClonedBricks.Value+1 local clone= workspace.DirtBlock:Clone() clone.Name= "DirtBlockClone" clone.Parent= workspace.DirtBlockCloneGroup clone.Position= Vector3.new(xBuild,yBuild-3,zBuild) --workspace.DirtBlockCloneGroup.DirtBlockClone:Destroy() end end if Mouse.TargetSurface== Enum.NormalId.Front then --print("front") if Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then workspace.numClonedBricks.Value= workspace.numClonedBricks.Value+1 local clone= workspace.DirtBlock:Clone() clone.Name= "DirtBlockClone" clone.Parent= workspace.DirtBlockCloneGroup clone.Position= Vector3.new(xBuild,yBuild,zBuild-3) --workspace.DirtBlockCloneGroup.DirtBlockClone:Destroy() end end if Mouse.TargetSurface== Enum.NormalId.Back then --print("back") if Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then workspace.numClonedBricks.Value= workspace.numClonedBricks.Value+1 local clone= workspace.DirtBlock:Clone() clone.Name= "DirtBlockClone" clone.Parent= workspace.DirtBlockCloneGroup clone.Position= Vector3.new(xBuild,yBuild,zBuild+3) --workspace.DirtBlockCloneGroup.DirtBlockClone:Destroy() end end if Mouse.TargetSurface== Enum.NormalId.Left then --print("left") if Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then workspace.numClonedBricks.Value= workspace.numClonedBricks.Value+1 local clone= workspace.DirtBlock:Clone() clone.Name= "DirtBlockClone" clone.Parent= workspace.DirtBlockCloneGroup clone.Position= Vector3.new(xBuild-3,yBuild,zBuild) --workspace.DirtBlockCloneGroup.DirtBlockClone:Destroy() end end if Mouse.TargetSurface== Enum.NormalId.Right then --print("right") if Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then workspace.numClonedBricks.Value= workspace.numClonedBricks.Value+1 local clone= workspace.DirtBlock:Clone() clone.Name= "DirtBlockClone" clone.Parent= workspace.DirtBlockCloneGroup clone.Position= Vector3.new(xBuild+3,yBuild,zBuild) --workspace.DirtBlockCloneGroup.DirtBlockClone:Destroy() end end end
One of my attempts to correct the issue was to include some sort of debounce, and prevent it from activating simultaneously in a one-action function (script for one of the sides shown; the other sides follow suit basically):
local rightClicked= false-- debounce initiation Mouse.Button2Down:connect( function() if not rightClicked then rightClicked= true if tostring(Mouse.Target)== "DirtBlockClone" then if Mouse.TargetSurface== Enum.NormalId.Top then --print("top") if Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then workspace.numClonedBricks.Value= workspace.numClonedBricks.Value+1 local clone= workspace.DirtBlock:Clone() clone.Name= "DirtBlockClone" clone.Parent= workspace.DirtBlockCloneGroup clone.Position= Vector3.new(Mouse.Target.Position.X,Mouse.Target.Position.Y+3,Mouse.Target.Position.Z) rightClicked= false end end end end end)
One of the reasons I've used the Mouse class in preference to a click detector was associated with not having to create the detector object for each clone.
Edit: Building the bricks from top or below seem to be fine, but any side causes error; I feel this has to do with where the mouse is hovering over, or that the pressed event is really firing more than once.