Casting fireball disables flames and fires it. (Not how it was intended.)

So I made a script where you press “Q” to turn your flames on, and then whilst it is on you can fire a fireball however while it does fire the fireball, it de-activates the flames then fires it.

Is anyone able to help…?

VIDEO:

(In the video when I cast the fireball it disables the fire then launches the fireball.)

LOCAL SCRIPT:
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local activated = false
local casting = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	
	if input.KeyCode == Enum.KeyCode.Q then
		activated = not activated
		ReplicatedStorage.Remotes.FlameOn:FireServer(activated)
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 and activated then
		casting = not casting
		ReplicatedStorage.Remotes.FlameOn:FireServer(activated, casting, mouse.Hit)
	end
end)
SERVER SCRIPT:
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local debounce = false
local casted = false
local hasActivated = false

ReplicatedStorage.Remotes.FlameOn.OnServerEvent:Connect(function(player, activated, casting, mouse)
	if debounce then return end
	
	local character = player.Character
	local fire = script.Fire:Clone()
	local fire2 = script.Fire:Clone()
	fire2.Name = "Fire 2"
	
	if activated and not hasActivated then
		hasActivated = true
		local weld = Instance.new("WeldConstraint")
		weld.Name = "Fire Weld"
		weld.Part0 = fire
		weld.Part1 = character["Right Arm"]
		fire.CFrame = character["Right Arm"].CFrame * CFrame.new(0, -0.65, 0)
		
		local weld2 = Instance.new("WeldConstraint")
		weld2.Name = "Fire Weld 2"
		weld2.Part0 = fire2
		weld2.Part1 = character["Left Arm"]
		fire2.CFrame = character["Left Arm"].CFrame * CFrame.new(0, -0.65, 0)
		
		fire.Parent = character
		fire2.Parent = character
		weld.Parent = fire
		weld2.Parent = fire2
	else
		character:FindFirstChild("Fire"):Destroy()
		character:FindFirstChild("Fire 2"):Destroy()
		hasActivated = false
	end
	
	-- for casting fireball
	if casting and not casted then
		casted = true
		local fireball = script.Fireball:Clone()
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = fireball
		weld.Part1 = character["Right Arm"]
		fireball.CFrame = character["Right Arm"].CFrame
		weld.Parent = fireball
		
		local fireCast = character.Humanoid:LoadAnimation(script.FireCast)
		fireCast:Play()
		fireCast:GetMarkerReachedSignal("Cast"):Connect(function()
			fireCast:AdjustSpeed(0)
			fireball.Parent = workspace
			wait(0.2)
			weld:Destroy()
			local bodyVelocity = Instance.new("BodyVelocity")
			bodyVelocity.Velocity = CFrame.new(fireball.Position, mouse.Position).lookVector * 100
			bodyVelocity.Parent = fireball
			fireCast:AdjustSpeed(1)
		end)
		
		fireball.Touched:Connect(function(hit)
			if hit:IsDescendantOf(character) or hit:FindFirstChild("Fire") then return end
			
			fireball:Destroy()
			
			if hit.Parent:FindFirstChild("Humanoid") then
				local target = hit.Parent
				target.Humanoid:TakeDamage(10)
				fireball:Destroy()
			end
		end)
		wait(1.2)
		casted = false
	end
	
end)

I’m pretty sure the issue here is that hasActivated is defined outside of the functions in your ServerScript. The sequence of events causing the described buggy behavior is likely as follows:

  1. You press Q to activate flames, hasActivated is toggled to true in the server script
  2. You then click, which calls the same RemoteEvent. As hasActivated is stored inside the script itself, hasActivated will still be true from when you first pressed Q.
  3. Because of this, the conditions of activated and not hasActivated in that first if statement aren’t met, and your code is falling back to the else statement at the bottom of the method, which disables flames.

I’d personally recommend reworking how data relating to these abilities is stored. If you have a data system in place (something like folders containing values that refer to player stats, etc.) then you’re already halfway to a rework–what I would do is make it so when a new Player joins the game, a Statuses folder (or something like that) is created w/ their UserId. When Q is pressed, it toggles a BoolValue “FlamesActive” or something like that. You can then reference that variable from anywhere else, which will likely make your life a whole lot easier down the road as your game gets bigger. In my humble opinion, data about what the player is doing should never be stored within a script itself in general, but especially if it needs to be referenced from multiple sources.