How to get total time mouse held down for

Hi. I am writing a script that detects how long you held the mouse for and I use it to tween a frames size. I want the frames size to be tweened when the player is holding down the mouse, not after they are done holding down the mouse.

Current script:

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if not Tool.Enabled then return end
	if Input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	if not LeftLeg:FindFirstChild("BallWeld") then return end
	if GameProcessed then return end
	
	Tool.Enabled = true
	MouseDown = true
	StartedDown = tick()
end)



UserInputService.InputEnded:Connect(function(Input, GameProcessed)
	if Input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	if not LeftLeg:FindFirstChild("BallWeld") then return end
	if GameProcessed then return end
	
	MouseDown = false
	Tool.Enabled = false
	Duration = tick() - StartedDown
	
	
	
	if Duration > 1 then
		Duration = 1
	end
	
	
	PowerBar:TweenSize(UDim2.new(Duration , 0 , 1 , 0) , "Out" , "Linear" , 0.4)
	
	if Ball then
		Sound:Play()
		RemoteEvent:Fire(Ball, Duration)
		task.wait(Cooldown)
		PowerBar:TweenSize(UDim2.new(0,0,0,29) , "Out" , "Linear" , 0.3)
		Tool.Enabled = true
	end
end)

Any help would be appriciated, thanks!

1 Like

Have 2 events for mouse down and mouse up. Have a variable that tracks the total time for you. When the mouse is down, set the total time to zero and start the time. When the mouse is up, display or set the variable of total time. Hope this logic helps :+1:

1 Like

But, I want to be able to get the total time being held down while the mouse is being held down, and use this information to tween a frame.

You’re better off not using tweens then.

Then how am I meant to change the position of the frame? I know a few games that have a “powerbar” that tweens the frame as you hold it down, until it reaches 100%

You would have to use os.clock():

local TimeHeldDown = os.clock()

Mouse.Button1Down:Connect(function()
    TimeHeldDonw = os.clock()
end)

Mouse.Button1Up:Connect(function()
     print(TimeHeldDown - os.clock())
end)

I believe you can get the position of the bar if you release the mouse as the bar is tweening. You can then use this information and multiply by the max power. Use the position based on scale.