Context Action Service Stops Working After Several Button Presses

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Homing attack when pressing Q.
  2. What is the issue? Include screenshots / videos if possible!

Homing attack stops after 3-6 presses

  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Tried UIS JumpRequest and InputBegin, Also tried the humanoid Jumped event.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Homing attack stops after 3-6 presses.

Not really much else to say, Though I am using a BodyPosition (Yes, i am aware i should be using AlignPosition). I am also using a custom rig, Though that shouldn’t impact it.
Here is the code for the homing attack:

local maxdist = 35
local curdist = math.huge
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local hrp = char.HumanoidRootPart or char:WaitForChild("HumanoidRootPart")
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local hum = char:FindFirstChildOfClass("Humanoid")
local jumped = false


local function getdist()
	local attackpart = nil
	
	for i, attack in pairs(workspace.Homing:GetChildren()) do
		local dist = (attack.Position - hrp.Position).Magnitude
		if dist < curdist and dist < maxdist then
			curdist = dist
			attackpart = attack
		end
	end

	
	if attackpart then
		print(attackpart)
		return attackpart
	else
		return
	end
end
task.spawn(function()
while task.wait(0.3) do
	hrp = char:FindFirstChild("HumanoidRootPart")
end
end)
local function HomingAttack(input)

	
	if jumped then 
		return
	else
		if hum.FloorMaterial == Enum.Material.Air then
			local attackpart = getdist()
			if attackpart then
				jumped = true

				local bp = Instance.new("BodyPosition",hrp)
				bp.Position = attackpart.Position
				bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
				bp.P = 100000
				if attackpart:GetAttribute("LetGo") == false then
					bp.ReachedTarget:Wait()
					bp:Destroy()
					hum:ChangeState(Enum.HumanoidStateType.Jumping)

					task.wait(0.2)
					jumped = false
				else
					task.wait(0.2)
					bp:Destroy()
					hum:ChangeState(Enum.HumanoidStateType.Jumping)
					hrp.Velocity = hrp.CFrame.LookVector * 70
					task.wait(0.2)
					jumped = false
				end
				
			end

		end	
	end

	
end


CAS:BindAction("HomingAttack",function()
task.spawn(function() HomingAttack() end) end,false,Enum.KeyCode.Q)
----------------------------------------------------------------------------------------------

Video upload seems broken. Take this test place instead.
Test.rbxl (991.1 KB)

local function getdist()
	local attackpart = nil
	
	for i, attack in pairs(workspace.Homing:GetChildren()) do
		local dist = (attack.Position - hrp.Position).Magnitude
		
		print(maxdist)
		print(curdist)
		
		if dist < maxdist then -- removed the check for dist being less than curdist
			curdist = dist
			attackpart = attack
		end
		
	end

	
	if attackpart then

		
		return attackpart
	else
		return
	end
end

Your checking if the distance is less than the current distance. But you keep setting the variable lower and lower. Until eventually you’ll have to be so close to the homing object that you can’t do it any more. Simply remove the check for it. or don’t set the variable. You could also reset the current distance somewhere else in the function / script

This works! Thank you very much!