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

How to make a tool do something constantly for as long as the mouse button is held down?

I'm trying to make a gun, but can't seem how to make it automatic. This is my trash script:

while true do
	script.Parent:Activate()
end

All the variables/raycasting is dealt with, and this is just really annoying! Is anyone able to help?

1 comment

0

Just detect whether the mouse is down or not and fire if it is

1 answer

Try this: Note had a little fun with the printout... you can delete them if you like lol

local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local ToolActive = false
local MouseHeldDown = false


Tool.Activated:connect(function()
	print("Woot! we\'ve been Activated!!!! YAAYY!!!")
	Mouse.Button1Down:Connect(function()
		MouseHeldDown = true
	end)
	Mouse.Button1Up:Connect(function()
		MouseHeldDown = false
	end)
end)
Tool.Deactivated:connect(function()
	print("Nooooo! we've been deactivated!..... *cries*")
	MouseHeldDown = false
end)

Tool.Equipped:connect(function() 
	print("WOH weve been selected o.0!!")
end)

Tool.Unequipped:Connect(function()
	print("Noooo!... I only farted you dont have to run away... oh god help mez pls!")
end)

local oldMouseHeld = nil
while true do
	--// only print out the change in mouse press!
	if(oldMouseHeld ~= MouseHeldDown) then
		print("MouseHeldDown is [",MouseHeldDown,"]")
		oldMouseHeld = MouseHeldDown
	end
	wait()
end

Just a little example on how to handle mouse held down. you can still activate your gun but check to see if the new varible MouseHeldDown is true or not before doing stuff!.

Hope this helps!

3 comments

0

oh! just encase, this in a Local script NOT server side!

0

Why are connecting events inside events

1

and i also see varying usage of Connect and connect

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