Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make this fire only when Mouse is down ?

local Tool = script.Parent
local Handle = Tool.Handle

local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent



Tool.Equipped:Connect(function(Mouse)
	
	Mouse.Button1Down:Connect(function()
	
	Handle.Touched:Connect(function(touch)
		if touch:IsA("Part") then
			print('g')
		end

		end)	
	end)

end)

I want the touched event to fire only when I press mouse down

2 comments

0

game.Players.LocalPlayer:GetMouse().Button1Down:Connect(function() -- code -- end)

0

^^^ He wants the Touched event to fire when the mouse is currently being held down, not as soon as it begins to be held down.

2 answers

✓ Accepted answer

This is your code:

Mouse.Button1Down:Connect(function()
	Handle.Touched:Connect(function(touch)
		if touch:IsA("Part") then
			print('g')
		end
	end)    
end)

When the left mouse button is pressed, it waits for the Handle to be touched. Of course, even if the mouse is released, it will still wait for the touched event to happen.

And you clearly want the touched event to only run if the left mouse button is currently being held down.

Handle.Touched:Connect(function(touch)
	if game.UserInputService:IsMouseButtonPressed(0) then
		if touch:IsA("Part") then
			print('g')
		end
	end
end)    

The :IsMouseButtonPressed() function of UserInputService will return true if the type of input you provide in the parentheses is currently being used. In this instance, you want the left mouse button, which is 0.

And for reference, this is the list of values that pertain to different types of input.

link

Here's the finished script.

local Tool = script.Parent
local Handle = Tool.Handle
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

Handle.Touched:Connect(function(touch)
	if game.UserInputService:IsMouseButtonPressed(0) and Tool.Parent = Character then
		if touch:IsA("Part") then
			print('g')
		end
	end
end)

To do that, you'll need some kind of variable to hold a true or false value so when the touched event if fired you can check if the variable is true or false.

Shown in this example here:

local tool = script.Parent
local handle = tool.Handle
local mouseDown = false

tool.Equipped:Connect(function()
	tool.Actived:Connect(function()
		mouseDown = true
	end)

	tool.Deactivated:Connect(function()
		mouseDown = false
	end)
end)

handle.Touched:Connect(function(hit)
	if hit:IsA("BasePart") then
		if mouseDown == true then
			print("g")
		end
	end
end)

2 comments

0

This solution looked to be fine at first, but it turns out there's a bit of a problem. No, I'm not talking about the small typo you made. The Deactivated event only fires when the left mouse button is released while the tool is equipped. A player could equip the tool, click, but unequip it and release and the deactivated event will not fire.

0

And this is only assuming that Overseer_Prince wants the tool to be equipped and the left mouse button to be currently held down for the touched code to run.

This discussion is preserved as it was. New answers, comments, and votes are closed.