Troubles with the targeting system of a magic attack that I tried to script

I wrote a script for a spell that inflicts pain on the target. :] How it works is, you click on a target, if that target is a player, it will place a selection box around the model. From there, if the client presses Q, it sends a remote event to the server and damages the target for as long as Q is held down. My problem is, if there’s a person who is already pain inflicting someone, if another client wants to pain inflict another person, they will be glitched(the animation plays, their movement slowed, but no damage to the target) because there is already someone pain inflicting. I am positive the problem is in the local script, so here it is. I only posted part of the local script that’s for this specific spell. I couldn’t try anything to fix it because there were no errors in the output, and I just had no idea why it was doing it. Here’s the glitch in action as well
https://gyazo.com/0a78bcb5b438d7c73fe4825733b4631e

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() 
local UIS = game:GetService('UserInputService')
local RS = game:GetService('ReplicatedStorage')

local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.new(186, 186, 186)
selection.Parent = player.PlayerGui

local qPressed = false

mouse.Button1Up:Connect(function()
	local target = mouse.Target
	
	------------------------------------------
	if target.Parent:IsA('Model')then
		if target.Parent:FindFirstChild('Humanoid')then
			selection.Adornee = target.Parent
			local animation = script.PainAnim
			local CastingAnim = player.Character.Humanoid:LoadAnimation(animation)
			CastingAnim.Looped = true
			------------------------------------
			-- a glitch where if a client Q's a person who is pain inflicting someone, it glitches the client. Can't also pain inflict another target while someone else is pain inflicting.
			UIS.InputBegan:connect(function(key,blah)
				if blah then return end
				if key.KeyCode == Enum.KeyCode.Q then
					if selection.Adornee == target.Parent and target.Parent.Humanoid.Health ~= 0 then
						local origin = player.Character.HumanoidRootPart.Position
						local destination = target.Parent.HumanoidRootPart.Position
						local ray = Ray.new(origin,(destination - origin))
						if math.floor((origin - destination).magnitude)<= 40 then
							--checking if they are not already being pain inflicted or on fire by incendia or ignited from theirself
							if target.Parent.HumanoidRootPart:FindFirstChild('higherRing') or target.Parent.HumanoidRootPart:FindFirstChild('Fire') or target.Parent.HumanoidRootPart:FindFirstChild('ignite')then return end
							if qPressed then return end
							print(target.Parent.Name)
							qPressed = true
							CastingAnim:Play()
							selection.Adornee = nil
							PainInflict:FireServer(target)
							while qPressed do
								wait()
								player.Character.Humanoid.WalkSpeed = 1.5
								game.Players.LocalPlayer.PlayerGui.Stamina['Sprinting Bar'].Disabled = true
								target.Parent.Humanoid.Died:connect(function()
									qPressed = false
									ItamiYame:FireServer(target)
								end)
							end
							CastingAnim:Stop()
							player.Character.Humanoid.WalkSpeed = 16
							game.Players.LocalPlayer.PlayerGui.Stamina['Sprinting Bar'].Disabled = false
						end
					end
				end
			end)
			UIS.InputEnded:connect(function(key,blah)
				if blah then return end
				if key.KeyCode == Enum.KeyCode.Q then
					if target.Parent.HumanoidRootPart:FindFirstChild('higherRing') and qPressed == true then
						qPressed = false
						selection.Adornee = nil
						ItamiYame:FireServer(target)
					end
				end
			end)
		end
	else
		target = nil
	end
end)