Holding mouse but returns false

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make m1s that work while im moving my character but if i move and hold down mouse it registers it as false.
  2. What is the issue? Include screenshots / videos if possible!
    Im moving and holding mouse but roblox thinks im not holding down my mouse
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I printed IsPc and it returned true so i printed down and it returned false despite me literally having my mouse held down.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- M1s
local LocalPlayer = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local m1Event = game.ReplicatedStorage:WaitForChild('M1')

local down = false
local cooldown = 0.2
local timeDown = 0
local UIS = game:GetService('UserInputService')
local connection

local values = LocalPlayer:WaitForChild('Values')
local m1Count = values:WaitForChild('M1s')
local oldValue = m1Count.Value

local playerGui = LocalPlayer:WaitForChild('PlayerGui')
local HUD = playerGui:WaitForChild('HUD')
local ContextActionService = game:GetService('ContextActionService')

local function IsPc()
	return UIS.MouseEnabled
end

local function IsMoving()
	return UIS:IsKeyDown(Enum.KeyCode.W) or UIS:IsKeyDown(Enum.KeyCode.A) or UIS:IsKeyDown(Enum.KeyCode.S) or UIS:IsKeyDown(Enum.KeyCode.D)
end

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) and IsPc() then
		down = true
	end
end)

UIS.InputEnded:Connect(function(input, processed)
	if processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 and IsPc() then
		down = false
	end
end)


local function DoM1()
	if down and IsPc() then
		print('Down')
		m1Event:FireServer()
	end
end

connection = game:GetService('RunService').RenderStepped:Connect(function(delta)	
	DoM1()
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

You don’t need so much code for such a simple thing.
This is to detect whether your mouse is held down or not:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local down = false

mouse.Button1Down:Connect(function()
	down = true
end)

mouse.Button1Up:Connect(function()
	down = false
end)

Hope it helps!

doesnt work when im walking :speaking_head: :speaking_head:

Have you used the code above? When walking, it works fine for me.

doesn’t work :frowning:

-- M1s
local LocalPlayer = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local m1Event = game.ReplicatedStorage:WaitForChild('M1')

local down = false
local cooldown = 0.2
local timeDown = 0
local UIS = game:GetService('UserInputService')
local connection

local values = LocalPlayer:WaitForChild('Values')
local m1Count = values:WaitForChild('M1s')
local oldValue = m1Count.Value

local playerGui = LocalPlayer:WaitForChild('PlayerGui')
local HUD = playerGui:WaitForChild('HUD')
local ContextActionService = game:GetService('ContextActionService')

local function IsPc()
	return UIS.MouseEnabled
end

local function IsMoving()
	return UIS:IsKeyDown(Enum.KeyCode.W) or UIS:IsKeyDown(Enum.KeyCode.A) or UIS:IsKeyDown(Enum.KeyCode.S) or UIS:IsKeyDown(Enum.KeyCode.D)
end

local mouse = LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
	if not IsPc() then return end
	
	down = true
end)

mouse.Button1Up:Connect(function()
	if not IsPc() then return end
	
	down = false
end)

local function DoM1()
	if down and IsPc() then
		print('Down')
		m1Event:FireServer()
	end
end

connection = game:GetService('RunService').RenderStepped:Connect(function(delta)	
	DoM1()
end)```

Just tried your script and it works for me even when walking/jumping/etc.

ok but it doesn’t work for me so fix it

Are you getting anything in your console?

Nah. nothing is printing so FIX my code please ive been stuck on this for 4 days straight

instead of UIS:IsMouseButtonDown() you should just check if the input type of the input is Mouse button 1.

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
       down = true
    end
end)

and apply the same logic to the InputEnded event

i already did EARLIER that you’re not fixing anything

Are you showing us the whole script or just a section of it? I’m asking this because there doesn’t seem to be anything written in it that could cause your problem to happen, although as @CloudyBoy27 correctly stated that does seem to be quite a lot of code to achieve the result you’re looking for if your end goal is to fire the M1 RemoteEvent when a player presses their MouseButton1. All you need to do really is this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")

local m1Event = ReplicatedStorage:WaitForChild("M1")

local function onMouseButton1(_, inputState)
	if inputState == Enum.UserInputState.Begin then
		m1Event:FireServer()

		print("m1Event fired successfully")
	end
end

ContextActionService:BindAction("MouseButton1", onMouseButton1, false, Enum.UserInputType.MouseButton1)

Edit: @112365595 I suspect you wish to repeatedly fire the m1Event while the MouseButton1 is held. I really don’t recommend you do this as it will cause lag while playing using the Roblox player instead of when testing in Studio unless you implement a debounce on the server-side where you have your OnServerEvent connnection for the RemoteEvent. If you insist on continuing to use this method though, this is what you need to do instead:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")

local m1Event = ReplicatedStorage:WaitForChild("M1")

local preRenderConnection = nil

local function onPreRender()
	m1Event:FireServer()
end

local function onMouseButton1(_, inputState)
	if inputState == Enum.UserInputState.Begin then
		preRenderConnection = RunService.PreRender:Connect(onPreRender)

		print("MouseButton1 loop activated")
	else
		if preRenderConnection then
			preRenderConnection:Disconnect()
			preRenderConnection = nil
		end

		print("MouseButton1 loop deactivated")
	end
end

ContextActionService:BindAction("MouseButton1", onMouseButton1, false, Enum.UserInputType.MouseButton1)

doesn’t work is it just a problem with my laptop

I would suggest carefully checking all the scripts you have at this point unfortunately, at least the ones where you’re handling player input

i tested it a new file with no scripts and it still didnt work when i was moving

Out of curiosity, did you modify the script I gave you in any way?

Edit: @112365595 As proof that the script I gave you should work, I made this demo place for you: MouseButton1Example.rbxl (54.1 KB)

As I mentioned I added a server-side debounce as lag protection, but if you insist on removing it then replace the code inside of the server Script with:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local m1Event = ReplicatedStorage.M1

local function onServerEvent()
	print(true)
end

m1Event.OnServerEvent:Connect(onServerEvent)

doesn’t work in ur game either.

It is a client-sided issue with you then. What device are you on? (What brand of laptop?)

1 Like

If you have one available, try connecting a desktop mouse to your laptop instead of using its trackpad