Hello, I've been developing a script that when the mouse left button is held down, a value is incremented until the left button is released. My goal is that while the mouse is down and simultaneously if the object that the mouse is hovering over changes, the value would be reset to zero, and would still increment. The following is my code/ attempt:
local Player= game.Players.LocalPlayer
local Mouse= Player:GetMouse()
local holdSwitch= 0
local holdVal= 0
--compares difference between object 1 and 2
local obj1= Mouse.Target.Position
local obj2= Mouse.Target.Position
local screenGUI= Instance.new("ScreenGui",game.Players.LocalPlayer.PlayerGui)
screenGUI.Name= "screenGUI"
--GUI for holding down mouse key
local textLabelHoldMouse= Instance.new("TextLabel",screenGUI)
textLabelHoldMouse.Name= "HoldMouse"
textLabelHoldMouse.Position= UDim2.new(0,10,0,400)
textLabelHoldMouse.Size= UDim2.new(0,150,0,25)
textLabelHoldMouse.BorderSizePixel= 0
textLabelHoldMouse.BackgroundTransparency= 1
textLabelHoldMouse.TextColor3= BrickColor.White().Color
textLabelHoldMouse.Text= "Hold: "
textLabelHoldMouse.TextXAlignment= "Left"
textLabelHoldMouse.TextTransparency= 0--toggle
--detection script
local vb= 0
Mouse.Button1Down:connect(function()
--with distance if; numMode indicates build or destroy
if workspace.numMode.Value== 0 and tostring(Mouse.Target)== "DirtBlockClone" and Player:DistanceFromCharacter(Mouse.Target.Position)< 16 then
Mouse.Button1Up:connect(function()--stops count and resets holdVal
holdSwitch= 1
holdVal= 0
--textLabelHoldMouse.Text= "Hold: "..holdVal
end)
holdSwitch= 0
while holdSwitch== 0 do
wait(0)
holdVal= holdVal+1
textLabelHoldMouse.Text= "Hold: "..holdVal
if not obj1== obj2 then
holdVal= 0
end
end
end
end)
So, I instantiate a comparison for each step of the wait interval, and it would test if the object target position has changed. I'm not sure what is going wrong.

2 comments
Also, you can make an assumption that the objects are Parts which are also clones with the same name. So one of the few things I thought of on how to differentiate between each clone was its position.
Anyways, I found out that it was a good idea to instantiate the variable within the while loop so I am not touching anything than the desired target beforehand otherwise it would immediately be a false statement.