Remote function returning nil

I am attempting to use a remote function to determine whether a hitbox is touched.

remote.OnServerInvoke = function(plr, action)
     if action == 1 then
          Debris:AddItem(hitbox, 0.2)
          hitbox.Touched:Connect(function(part)
                 if part.Parent:FindFirstChild(“Humanoid”) then
                      return true
                 end
          end)
     else if action == 2 then
           Debris:AddItem(hitbox, 0.2)
           hitbox.Touched:Connect(function(part)
                 if part.Parent:FindFirstChild(“Humanoid”) then
                      return true
                 end
            end)
     end
    delay(0.2, function()
           return false
    end)
end

When I set a variable to the call

local callback = remote:InvokeServer(action)
print(callback)

callback is always nil

I saw another post that mentioned scope, but I wasn’t sure how this applied to my problem, as there is always a path to a return statement.

Thanks in advance.

When you use return, it will always return out of the most recent function.

Since you’re connecting a second function to the touched event, the return will exit out of that new function. Same for the delay function.

Here's one way to work around this:
remote.OnServerInvoke = function(plr, action)
	local result = false -- Define the result outside of the touched connection so it isn't confined to that function
	if action == 1 or action == 2 then -- If you're doing the same thing whether action == 1 or 2, then you don't need two if statements
		Debris:AddItem(hitbox, 0.2)
		hitbox.Touched:Connect(function(part)
			if part.Parent:FindFirstChild(“Humanoid”) then
				result = true
			end
		end)
	end
	
	task.wait(0.2) -- Give the hitbox time to be touched
	
	return result -- Return result; it will be true if hitbox was touched
end