Tool not working

I’m trying to make a flamethrower tool, where if you click it one time, it turns the flame particles and lights on, then when you click again, it stops. The stopping works, it’s just starting the flames that don’t.

The script
local Handle = script.Parent
local Tool = script.Parent.Parent
local IsEnabled = 0
Tool.Activated:connect(function()
	if IsEnabled == 0 then
		Handle.Flame1.Enabled = true
		Handle.Flame2.Enabled = true
		Handle.Flame3.Enabled = true
		Handle.FlameLight1.Enabled = true
		Handle.FlameLight2.Enabled = true
		Handle.FlameLight3.Enabled = true
		IsEnabled = 1
	end
	if IsEnabled == 1 then
		Handle.Flame1.Enabled = false
		Handle.Flame2.Enabled = false
		Handle.Flame3.Enabled = false
		Handle.FlameLight1.Enabled = false
		Handle.FlameLight2.Enabled = false
		Handle.FlameLight3.Enabled = false
		IsEnabled = 0
	end
end)

You’ll want to use an elseif statement here.

Basically, what’s happening is that IsEnabled is initially set to 0, and when the tool is activated, it enables everything, and then sets IsEnabled to 1, which is fine. The problem lies in the fact that it continues running the code, and the next if statement runs, and is true, so it then disables everything and sets IsEnabled to false. There are numerous ways to fix this, such as using an elseif statement. Personally, what I would do is this:

local Handle = script.Parent
local Tool = script.Parent.Parent
local IsEnabled = false -- Instead of having it as an integer, we use a boolean instead.
Tool.Activated:connect(function()
	IsEnabled = not IsEnabled -- This basically makes it toggle, meaning when IsEnabled is true, it becomes false, and when IsEnabled is false, it becomes true.
	-- Enable/disable
	Handle.Flame1.Enabled = IsEnabled
	Handle.Flame2.Enabled = IsEnabled
	Handle.Flame3.Enabled = IsEnabled
	Handle.FlameLight1.Enabled = IsEnabled
	Handle.FlameLight2.Enabled = IsEnabled
	Handle.FlameLight3.Enabled = IsEnabled
end)

You could also use an elseif statement like so:

if IsEnabled == 0 then
	-- Enable
elseif IsEnabled == false then
	-- Disable
end

I hope this helps!

1 Like