How to check if player mouse button is still down after x seconds?

local CanDrag=true
local Player=game.Players.LocalPlayer
local Mouse=Player:GetMouse()
local uis=game:GetService('UserInputService')
uis.InputBegan:Connect(function(inp, p)
	if inp.UserInputType == Enum.UserInputType.MouseButton1 and p==false and Mouse.Target ~=nil and Player:FindFirstChild("Tool") == nil and CanDrag~=false then
		CanDrag=true
		coroutine.wrap(function()
			local timer = tick()
			repeat
				if tick()-timer >= 1 then-- Change time preference here
					Drag()--name of the function specifically and values to use
				end
				wait()
			until CanDrag == false
		end)()
	end
end)

Strangely enough this works for me, theres something wrong on your behalf.

local MouseDown = false
local timeToWait = 5
--in your script with UserInputService, connect to InputEnded:
if inputObject.UserInputType==Enum.UserInputType.Mouse then
MouseDown=false
end
--in your InputBegan section with the mouse, do this:
if inputObject.UserInputType==Enum.UserInputType.Mouse then
MouseDown = true
local t = 0
while MouseDown and t<timeToWait do
t+=task.wait()
end
if t>=timeToWait then
--the mouse has been held down for [timeToWait] seconds.
end
end
1 Like

It is a while loop. There is no need since the condition checks for this:

1 Like

This solution kind of works, but I can no longer drop the part after it is picked up, it just stays there forever. Here is my current code;

uis.InputEnded:Connect(function(inputObject, proc)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 and not proc then
		MouseDown=false
		print("Ended")
		if Mouse.Target == selectedPart then
			if Dragger.Weld.Part1 == selectedPart then
				R = game.ReplicatedStorage.DragEvents.SetDrag:InvokeServer(Dragger.Weld.Part1, false)
				if R == true then
					selectedPart = nil
					Dragger.Weld.Part1 = nil
					Dragger.Parent = nil
					RotY = 0
					Y = 0
					X = 0
				end
			end
		end
	end
end)

uis.InputBegan:Connect(function(inputObject, proc)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 and not proc then
		MouseDown = true
		local t = 0
		while MouseDown and t<timeToWait do
			t+=task.wait()
		end
		if t>=timeToWait then
			print("Began")
			if Mouse.Target ~= nil and Player:FindFirstChild("Tool") == nil then
				if Mouse.Target:FindFirstChild("CanDrag") ~= nil then
					R = game.ReplicatedStorage.DragEvents.SetDrag:InvokeServer(Mouse.Target, true)
					if R == true then
						Dragger.CFrame = Mouse.Hit
						Dragger.Weld.Part1 = Mouse.Target
						selectedPart = Mouse.Target
						Dragger.Parent = game.Workspace
					end
				end
			end
		end
	end
end)

You should use InputBegan and InputEnded, also tick(). If you don’t know how to use them tell me

I have tried that but cannot find a solution to it using tick(). Here is my current code;

local Mouse = game.Players.LocalPlayer:GetMouse()
local Dragger = script.Parent.Dragger
local selectedPart
local uis = game:GetService("UserInputService")

Dragger.Parent = nil
Shift = false
Y = 0
RotY = 0
X = 0
RotX = 0
local Player = game.Players.LocalPlayer.Character
while Player == nil do
	Player = game.Players.LocalPlayer.Character
	wait()
end
--
YSpeed = 3
XSpeed = 3
--
local MouseDown = false

uis.InputBegan:Connect(function(input, proc)
	if not proc then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			MouseDown = true
			if Mouse.Target ~= nil and Player:FindFirstChild("Tool") == nil and MouseDown == true then
				if Mouse.Target:FindFirstChild("CanDrag") ~= nil then
					R = game.ReplicatedStorage.DragEvents.SetDrag:InvokeServer(Mouse.Target, true)
					if R == true then
						Dragger.CFrame = Mouse.Hit
						Dragger.Weld.Part1 = Mouse.Target
						selectedPart = Mouse.Target
						Dragger.Parent = game.Workspace
					end
				end
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, proc)
	if not proc then 
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			MouseDown = false
			print(Mouse.Target and selectedPart)
			if Dragger.Weld.Part1 == selectedPart and MouseDown == false then
				R = game.ReplicatedStorage.DragEvents.SetDrag:InvokeServer(Dragger.Weld.Part1, false)
				if R == true then
					selectedPart = nil
					Dragger.Weld.Part1 = nil
					Dragger.Parent = nil
					RotY = 0
					Y = 0
					X = 0
				end
			end
		end
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		Shift = true
		Player.Humanoid.WalkSpeed = 0
	end
	if Key.KeyCode == Enum.KeyCode.W and Shift == true then
		Y = Y - YSpeed
	elseif Key.KeyCode == Enum.KeyCode.S and Shift == true then
		Y = Y + YSpeed
	end
	if Key.KeyCode == Enum.KeyCode.A and Shift == true then
		X = X + XSpeed
	elseif Key.KeyCode == Enum.KeyCode.D and Shift == true then
		X = X - XSpeed
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift then
		Shift = false
		Player.Humanoid.WalkSpeed = 16
		Y = 0
		X = 0
	end
	if Key.KeyCode == Enum.KeyCode.W and Shift == true then
		Y = Y + YSpeed
	elseif Key.KeyCode == Enum.KeyCode.S and Shift == true then
		Y = Y - YSpeed
	end
	if Key.KeyCode == Enum.KeyCode.A and Shift == true then
		X = X - XSpeed
	elseif Key.KeyCode == Enum.KeyCode.D and Shift == true then
		X = X + XSpeed
	end
end)

--

while true do
	wait()
	if Dragger.Weld.Part1 ~= nil then
		RotY = RotY + Y
		RotX = RotX + X
		CF1 = CFrame.new(Player.Head.CFrame.Position, Vector3.new(Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z))
		CF2 = CF1 * CFrame.new(0, 0, -10)
		Dragger.BPos.Position = Vector3.new(CF2.X, CF2.Y, CF2.Z)
		Dragger.BGyro.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(RotY), math.rad(RotX), 0)
	end
end

Everything works, except for when I click and unclick very fast. This causes the Dragger to glitch out and set the welds Part1 to itself instead of nil, among other issues.

Alright first of all, make sure you remove the while true do loop from that script,
Here’s the script that I suggest you to try

local realTime = tick()
local holdTime = 5

UIS.InputBegan:Connect(function(input, notTyping)
if notTyping then
if input.UserInputType == Enum.UserInputType.MouseButton1 then

realTime = tick() --it starts ticking from 0 again,

end
end
end)

UIS.InputEnded:Connect(function(input, notTyping)
if notTyping then
if input.UserInputType == Enum.UserInputType.MouseButton1 then

if realTime - tick() <= holdTime then --if player hold the mouse button less than 5 secs or for 5 secs
print("test success")
end

end
end
end)

I’ve tried to explain how to use tick and uis here, your code is a little complicated tho. You might wanna fix that.

Nothing prints, even if I change the holdTime variable to something lower.

Sorry I wrote it wrong

Use this please

local realTime = tick()
local holdTime = 5

UIS.InputBegan:Connect(function(input, notTyping)
if notTyping then
if input.UserInputType == Enum.UserInputType.MouseButton1 then

realTime = tick() --it starts ticking from 0 again,

end
end
end)

UIS.InputEnded:Connect(function(input, notTyping)
if notTyping then
if input.UserInputType == Enum.UserInputType.MouseButton1 then

print(realTime - tick())
if realTime - tick() <= holdTime then --if player hold the mouse button less than 5 secs or for 5 secs
print("test success")
else
print("test2")
end

end
end
end)

Tell me the output results

Fixed the main issue with the code where as you were checking the gameProccessed wrong; here is the current code;

local realTime = tick()
local holdTime = 1
local UIS = game.UserInputService

UIS.InputBegan:Connect(function(input, typing)
	if not typing then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			realTime = tick() --it starts ticking from 0 again,
		end
	end
end)

UIS.InputEnded:Connect(function(input, typing)
	if not typing then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print(realTime - tick())
			if realTime - tick() <= holdTime then --if player hold the mouse button less than 5 secs or for 5 secs
				print("test success")
			else
				print("test2")
			end

		end
	end
end)

And it prints test success, however I need this on the inputbegan not ended.

check this out, it might help you im gonna go sleep now sorry

Just use UserInputService.InputChanged, this will help with Detecting whether they started Holding down the Mouse button or not.

local holdTime = 1

local holdStart = tick()
local holding = false -- to check if they are holding down

UserInputService.InputChanged:Connect(function(input, gpe)
    if gpe then return end
    
    if input.UserInputType = Enum.UserInputType.MouseButton1 then
        holding = not holding -- not true = false / not false = true
        holdStart = if holding then tick() else holdStart
        -- using an if statement to assign a value to a variable
        -- in this case, its the time the event happened
        
        if (tick() - holdStart) >= holdTime then -- Checking the Elasped Time
            print("held") -- Confirms it was Held
        end
    end

end)

FINALLY got a fix!! After a few tries of using different scripts, I came out with this addition -

if Dragger.Weld.Part1 == Dragger or game.Workspace.World.Baseplate then
				selectedPart = nil
				Dragger.Parent = nil
				Dragger.Weld.Part1 = nil
				RotY = 0
				Y = 0
				X = 0
			end

Which completely solves my problem!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.