Flamethrower randomly detecting the mouse is released

Heya.

As the title says, I’ve made a flamethrower although it randomly decides to release the mouse and stops the flames from spewing out.

local UIS = game:GetService("UserInputService")
local Ticket = 0 --Using a ticket service so it won't be confused by normal clicks I guess.
local timeHeld = 0.2
local isHolding = false

function MouseHeld()
	isHolding = true
	
	while isHolding do
		print("Fire!")
		
		Tool.Flamethrower.Attachment.Flames.Enabled = true
		Tool.Flamethrower.Attachment.Smoke.Enabled = true
		task.wait()
	end
end

function MouseReleased()
	if not isHolding then return end
	isHolding = false
	
	print("Done :)")
	
	Tool.Flamethrower.Attachment.Flames.Enabled = false
	Tool.Flamethrower.Attachment.Smoke.Enabled = false
end

UIS.InputBegan:Connect(function(i,gp)
	if gp then return end
	
	if i.UserInputType == Enum.UserInputType.MouseButton1 then
		Ticket = Ticket + 1
		local CurrentTicket = Ticket
		
		if timeHeld > 0 then
			delay(timeHeld, function()
				if Ticket == CurrentTicket then
					MouseHeld()
				end
			end)
		else
			MouseHeld()
		end
	end
end)

UIS.InputEnded:Connect(function(i,gp)
	if gp then return end
	
	Ticket = Ticket + 1
	MouseReleased()
end)

You never check which input has ended in the UIS.InputEnded function, meaning if any input were to end, the MouseReleased() function would run, even if the mouse was still holding.

Replace the UIS.InputEnded function with this piece of code:

UIS.InputEnded:Connect(function(i,gp)
	if gp then return end
	
	if i.UserInputType == Enum.UserInputType.MouseButton1 then
		Ticket = Ticket + 1
		MouseReleased()
	end
end)

It works, thanks!

Now I can script it so it can commit arson :smiley:

1 Like

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