Why is this firing the InputEnd first?

As the title says, the InputEnd is fired first. This only happens when I just do a simple click, nothing else. Any fixes?

Server:

local pressing = 0
local playersFireball
local maxSize = Vector3.new(5,5,5)

function KeyPress(player, state, mouse)
	print('okay')
	if state == "Began" then
		KeyDown(player, mouse)
		print('began')
		wait()
	else
		KeyUp(player)
		print('ended')
	end
end

function KeyDown(player, mouse)
	if pressing == 0 then
		pressing = 1
		local hrp = player.Character.HumanoidRootPart
		hrp.Anchored = true
		wait(0.5)
		local fireball = Instance.new("Part")
		fireball.Parent = workspace.Projectiles
		fireball.Shape = "Ball"
		--fireball.Size = Vector3.new(0.1, 0.1, 0.1)
		fireball.CanCollide = false
		fireball.Anchored = true
		fireball.CFrame = hrp.CFrame*CFrame.new(0, 0, -5)
		local damage = Instance.new("NumberValue");
		damage.Name = "Damage"
		damage.Value = 0
		damage.Parent = fireball
		local bv = Instance.new("BodyVelocity")
		bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bv.Velocity = mouse.lookVector * 250
		bv.Parent = fireball
		playersFireball = fireball
		player.Character.Humanoid.WalkSpeed = 0
		while pressing == 1 do
			print(pressing)
			fireball.Size = fireball.Size + Vector3.new(0.1,0.1,0.1)
			damage.Value = damage.Value + 10
			fireball.CFrame = hrp.CFrame*CFrame.new(0, 0, -5)
			bv.Velocity = mouse.lookVector * 250
			if fireball.Size.X >= maxSize.X then
				KeyUp(player)
			end
			wait(0.1)
		end
	end
end

function KeyUp(player)
	if pressing == 1 then
		pressing = 0
		print('up')
		local fireball = playersFireball
		fireball.Anchored = false
		player.Character.HumanoidRootPart.Anchored = false
		player.Character.Humanoid.WalkSpeed = 16
	end
end

script.Parent.OnServerEvent:Connect(KeyPress)

Local:

local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = true
local held = false
local UIS = game:GetService("UserInputService")
local equip = false

tool.Equipped:Connect(function(mouse)
	equip = true
end)

UIS.InputEnded:Connect(function(Input, gameProcessedEvent)
	if not gameProcessedEvent and equip == true then
		local KeyCode = Input.UserInputType
		if KeyCode == Enum.UserInputType.MouseButton1 then
			script.Parent.Fireball:FireServer("Ended", mouse.Hit)
		end
	end
end)
	
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
	if not gameProcessedEvent and equip == true then
		local KeyCode = Input.UserInputType
		if KeyCode== Enum.UserInputType.MouseButton1 and debounce == true then
			debounce = false
			script.Parent.Fireball:FireServer("Began", mouse.Hit)
			wait(3) debounce = true
		end
	end
end)
	
tool.Unequipped:Connect(function()
	equip = false
end)

Outputs:

okay(x2)
up
Players.Math_Solvers.Backpack.Fireball.Fireball.Script:59: attempt to index local 'fireball' (a nil value)
began

You should use inputbegan before inputended

Still the same problem happens after switching the order in the local script

I think you meant Input.KeyCode, not just keycode

Edit: oops, didn’t read the code properly

No, I think that part is perfectly fine…

I don’t know necessarily what the problem is, however ContextActionService can be binded to inputs and calls the function binded with the current UserInputState - that saves you from having to get the state.

Also, the pressing variable can be overwritten by another player in the Script.
You may want to create a individual pressing variable for each player, maybe store them in a dictionary.

Alright, thanks! The script is actually inside a gear.

To fix this, put the InputEnded function right before the end with the ) of InputBegan