Button1Down not functioning on tool?

I’m trying to make a simple Hold Down to Fire Event, however GetMouse() does not seem to recognize the buttons i am pressing regardless when i have the tool equipped.

local __MOUSE = __LOCALPLAYER:GetMouse()
local __TOOL = script.Parent 
local __TIME = __TOOL:WaitForChild("__TIMEITTAKES")
local __EVENT = __TOOL:WaitForChild("__PLACE")
local __ISHOLDING : BoolValue? = false
__MOUSE.Button2Down:Connect(function()
	__ISHOLDING = true
end)

__MOUSE.Button2Up:Connect(function()
	__ISHOLDING = false
end)

coroutine.wrap(function()
	while __ISHOLDING == true do
		task.wait()
		__TIME.Value += 1
		print(__TIME.Value)
		if __TIME.Value == 10 then
			__EVENT:FireServer()
			break
		end
	end
end)()

You’re using Button2Down and Button2Up, is this intentional?

Yes i even tried Button1 and it still wouldnt work.

Ah, hold on, I see the problem.

I believe it is this. The while loop never runs because __ISHOLDING is false in the beginning.

local __MOUSE = __LOCALPLAYER:GetMouse()
local __TOOL = script.Parent 
local __TIME = __TOOL:WaitForChild("__TIMEITTAKES")
local __EVENT = __TOOL:WaitForChild("__PLACE")
local __ISHOLDING : BoolValue? = false

__MOUSE.Button2Down:Connect(function()
	__ISHOLDING = true
	
	while __ISHOLDING == true do
		task.wait()
		__TIME.Value += 1
		print(__TIME.Value)
		if __TIME.Value == 10 then
			__EVENT:FireServer()
			break
		end
	end
end)

__MOUSE.Button2Up:Connect(function()
	__ISHOLDING = false
end)

That worked but the loop doesnt break when the value reaches 10

Not sure why it isn’t breaking, but just add another condition.

local __MOUSE = __LOCALPLAYER:GetMouse()
local __TOOL = script.Parent 
local __TIME = __TOOL:WaitForChild("__TIMEITTAKES")
local __EVENT = __TOOL:WaitForChild("__PLACE")
local __ISHOLDING : BoolValue? = false

__MOUSE.Button2Down:Connect(function()
	__ISHOLDING = true
	
	while __ISHOLDING == true and __TIME.Value < 10 do
		task.wait()
		__TIME.Value += 1
		print(__TIME.Value)
		if __TIME.Value == 10 then
			__EVENT:FireServer()
		end
	end
end)

__MOUSE.Button2Up:Connect(function()
	__ISHOLDING = false
end)

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